Javascript日期格式化指定格式的字符串实现
代码部分
TypeScript
/**
* format a Date object
* 将 Date 转化为指定格式的String
* @param {Date} date 源日期对象
* @param {string} pattern 匹配模式
* @returns {string} 格式化结果
*/
fmtDate(date: Date, pattern: string) {
return pattern
.replace(/yyyy/, date.getFullYear().toString())
.replace(/MM/, this.fillZero(date.getMonth() + 1, 'l', 2))
.replace(/dd/, this.fillZero(date.getDate(), 'l', 2))
.replace(/hh/, this.fillZero(date.getHours(), 'l', 2))
.replace(/mm/, this.fillZero(date.getMinutes(), 'l', 2))
.replace(/ss/, this.fillZero(date.getSeconds(), 'l', 2))
.replace(/S/, date.getMilliseconds().toString());
}
Javascript
/**
* format a Date object
* 将 Date 转化为指定格式的String
* @param {Date} date 源日期对象
* @param {string} pattern 匹配模式
* @returns {string} 格式化结果
*/
Aqua.prototype.fmtDate = function (date, pattern) {
return pattern
.replace(/yyyy/, date.getFullYear().toString())
.replace(/MM/, this.fillZero(date.getMonth() + 1, 'l', 2))
.replace(/dd/, this.fillZero(date.getDate(), 'l', 2))
.replace(/hh/, this.fillZero(date.getHours(), 'l', 2))
.replace(/mm/, this.fillZero(date.getMinutes(), 'l', 2))
.replace(/ss/, this.fillZero(date.getSeconds(), 'l', 2))
.replace(/S/, date.getMilliseconds().toString());
};
补零函数 Typescript
/**
* fill 0 to a number
* 数字补零
* @param {number} src 源数字
* @param {string} direction 方向 l r
* @param {number} digit 补零后的总位数
* @returns {string} 结果
*/
fillZero(src: number, direction: string, digit: number) {
let count: number = digit - src.toString().length;
let os = new Array(count + 1).join('0');
if (direction !== 'r') {
return os + src;
}
return src + os;
}
javascript
/**
* fill 0 to a number
* 数字补零
* @param {number} src 源数字
* @param {string} direction 方向 l r
* @param {number} digit 补零后的总位数
* @returns {string} 结果
*/
Aqua.prototype.fillZero = function (src, direction, digit) {
var count = digit - src.toString().length;
var os = new Array(count + 1).join('0');
if (direction !== 'r') {
return os + src;
}
return src + os;
};
原理很简单,就不写了
欢迎查看我的GitHub
https://github.com/rocketRobin/aqua-toolbox
Javascript日期格式化指定格式的字符串实现的更多相关文章
- 一个JavaScript日期格式化扩展函数
我们都知道在Java和PHP语言中,有专门用于格式化日期对象的类和函数,例如Java中的DateFormat等等,通过这些类和函数,我们可以方便的将一个日期对象按照格式的要求输出为字符串,例如对于同一 ...
- 使用指定格式的字符串变量格式化日期字符串,DateAndTime取时间间隔
private void btn_GetTime_Click(object sender, EventArgs e) { lab_time.Text = DateTime.Now.ToString(& ...
- Javascript 日期格式化
Javascript 日期格式化 需求: 给出:日期 .格式,根据日期格式进行输出. Date.prototype.Format = function (fmt) { //author: meizz ...
- julia生成指定格式的字符串.jl
julia生成指定格式的字符串.jl """ julia生成指定格式的字符串.jl http://bbs.bathome.net/thread-39829-1-1.htm ...
- JavaScript 日期格式化 简单有用
JavaScript 日期格式化 简单有用 代码例如以下,引入jquery后直接后增加下面代码刷新可測试 Date.prototype.Format = function (fmt) { //auth ...
- javascript日期格式化方法汇总
本文给大家汇总介绍了javascript格式化日期时间的几种常用方法,个人对最后一种个性化输出时间比较有兴趣,基本上只要项目中能用到都是使用这种,推荐给小伙伴们. 方法一: ? 1 2 3 4 5 6 ...
- (转)javascript日期格式化扩展
转自:http://blog.csdn.net/vbangle/article/details/5643091 javascript Date format(js日期格式化) 方法一:这个很不错, ...
- Javascript 日期格式化 相关操作
1.相关扩展函数 //--------------------------------------------------- // 判断闰年 //--------------------------- ...
- SQL 日期格式化与格式转化
日期格式化 Select CONVERT(varchar(), GETDATE(), ): :57AM Select CONVERT(varchar(), GETDATE(), ): // Selec ...
随机推荐
- Linux中cat、more、less、tail、head命令的区别
一.cat 显示文件连接文件内容的工具 cat 是一个文本文件(查看)和(连接)工具,通常与more搭配使用,与more不同的是cat可以合并文件.查看一个文件的内容,用cat比较简单,就是cat后面 ...
- Linux 分区的概念
事实上无论是linux 还是 window 都必须遵循以下分区的规则 分区类型 1.主分区:最多只能四个. 2.扩展分区: - 最多只能一个 - 主分区加扩展分区最多只能有四个 - 不能写入数据,它存 ...
- iOS9网络适配
今天升级Xcode7.0,发现网络访问失败. 输出错误信息:The resource could not be loaded because the App Transport Security po ...
- cpptoolstip界面提示库使用
很多时候写一些界面程序,需要实时的给用户提示,每次都弹出一个messagebox挺烦的,状态栏又怕人看不到,最后找了一个相关的类,显示效果不错,分享一下,效果如下 下载库下来之后对应八个文件 将八个文 ...
- iReport折线图
1.拖动组件面板chart到Summary 2.右击-->chart data 单出 3. 4.X轴和Y轴必须是数字 series:系列 连续,串联 category:类型.部门. ...
- LearningDocker--Chapter3--Building images
This chapter is quite different from the earlier ones, and it is in this chapter to clearly describe ...
- Lua C Api lua_gettable 、lua_settable 、lua_next 使用详解
之前一直没理清lua_gettable和lua_settable的使用,今天理清了,顺便就做下笔记了.1.lua_gettable void lua_gettable (lua_State *L, i ...
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
提示:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'.前两天也出现过这个问题,网上找了一个比 ...
- 处理JSON格式的数据
JSON格式的数据是最常用的数据格式,处理方法的选择就显得比较重要了.我常用的一种是用对象来接收,然后保存在数组中,需要时直接从数组中取值.下面列出一个小例子. .h文件中: #import < ...
- MPU6050程序(转)
源:MPU6050程序 初始化定义 #ifndef _MPU6050_H #define _MPU6050_H #define PORT_USED 0 #define MPU6050_ADDRESS_ ...