js根据时间戳获取格式化日期
2017-09-04 16:01
369 查看
js里面没有项php里面的date函数,可以直接获取格式化的日期。需要自己根据date对象分别去获取然后格式化。
function getFormatDate(timestamp) {
timestamp = parseInt(timestamp + '000');
var newDate = new Date(timestamp);
Date.prototype.format = function (format) {
var date = {
'M+': this.getMonth() + 1,
'd+': this.getDate(),
'h+': this.getHours(),
'm+': this.getMinutes(),
's+': this.getSeconds(),
'q+': Math.floor((this.getMonth() + 3) / 3),
'S+': this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp('(' + k + ')').test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ('00' + date[k]).substr(('' + date[k]).length));
}
}
return format;
}
return newDate.format('yyyy-MM-dd h:m');
}
示例:
function getFormatDate(timestamp) {
timestamp = parseInt(timestamp + '000');
var newDate = new Date(timestamp);
Date.prototype.format = function (format) {
var date = {
'M+': this.getMonth() + 1,
'd+': this.getDate(),
'h+': this.getHours(),
'm+': this.getMinutes(),
's+': this.getSeconds(),
'q+': Math.floor((this.getMonth() + 3) / 3),
'S+': this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp('(' + k + ')').test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ('00' + date[k]).substr(('' + date[k]).length));
}
}
return format;
}
return newDate.format('yyyy-MM-dd h:m');
}
示例:
相关文章推荐
- js 设置获取当前时间后30 的日期,并格式化
- js获取日期、时间、日期常用方法、时间计算并格式化和实现电子表
- (随笔)js获取当前时间并格格式化当前日期 获取date天后的日期(2014年11月27日 16:31:49)
- Js获取当前日期时间及格式化操作
- js获取当前时间以及日期格式化
- js根据选择日期获取对应时间
- Js获取当前日期时间及格式化操作
- JS获取当前日期时间及JS日期格式化
- 【转载】js获取当前日期,格式化成xxxx-xx-xx的格式(根据情况补零)
- Js获取当前日期时间及格式化操作
- 【转】Js获取当前日期时间及格式化操作
- Js获取当前日期时间及格式化操作
- 根据日期字符串获取星期几,日期获取星期,时间获取星期,js获取星期
- Js获取当前日期时间及格式化操作
- js 根据时间范围获取之间的日期
- js得到连两个时间段之间所有的年-月 获取当前日期 格式化日期 根据指定日期得到该日期该月份的最后一天
- Js获取当前日期时间及格式化操作
- JS日期,JS获取当前日期时间,js日期格式化
- JS获取当前日期和时间的方法,并按照YYYY-MM-DD格式化
- js根据当前时间获取指定范围内的时间段日期