javascript 日期Date转化成字符串 的各种方式
2018-02-07 22:46
274 查看
1、在JavaScript里面扩充Date的prototype:Date.prototype.format = function(format) { var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
4000
"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;
}
然后使用:
var newDate = new Date();
newDate.setTime(data.msg.post_time*1000);
newDate.format('yyyy/MM/dd hh:mm:ss'); 2006/07/02 08:09:04newDate.format('yyyy-MM-dd'); 2006-07-02
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
4000
"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;
}
然后使用:
var newDate = new Date();
newDate.setTime(data.msg.post_time*1000);
newDate.format('yyyy/MM/dd hh:mm:ss'); 2006/07/02 08:09:04newDate.format('yyyy-MM-dd'); 2006-07-02
相关文章推荐
- javascript转换日期字符串---转化为Date对象---再转化为时间戳
- js字符串日期yyyy-MM-dd转化为date示例代码
- java ------SimpleDateFormat------ 实现字符串与日期类型互相转化
- js字符串日期yyyy-MM-dd转化为date示例代码
- js 字符串日期 yyyy-MM-dd 转化为 date
- Javascript:日期字符串(DateStr)与日期对象(Date)之间的转换
- [转]js 字符串日期 yyyy-MM-dd 转化为 date
- js 字符串日期 转化为 date
- Javascript:日期字符串(DateStr)与日期对象(Date)之间的转换
- javascript--转化字符串为日期类型
- JavaScript通过Date-Mask将日期转换成字符串的方法
- JavaScript中扩展字符串与Date对象之间的转换,支持大多数日期格式的字符串(包括中文)
- JavaScript日期字符串转为Date对象
- 字符串日期转化为oracle.jbo.domain.Date和oracle.jbo.domain.Timestamp类型
- JavaScript中扩展字符串与Date对象之间的转换,支持大多数日期格式的字符串(包括中文)
- javascript转换日期字符串为Date对象
- js字符串日期yyyy-MM-dd转化为date示例代码
- js字符串日期yyyy-MM-dd转化为date示例代码
- JavaScript基础 Date(日期字符串 不包括时分秒) 不指定时分秒的时候 系统默认是0:0:0
- js 字符串日期 yyyy-MM-dd 转化为 date