从 JS 日期对象获取 YYYYMMDD 格式的字符串? [英] Get String in YYYYMMDD format from JS date object?
本文介绍了从 JS 日期对象获取 YYYYMMDD 格式的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 JS 将 date object
转换为 YYYYMMDD
格式的字符串.有没有比连接 Date.getYear()
、Date.getMonth()
和 Date.getDay()
更简单的方法?
I'm trying to use JS to turn a date object
into a string in YYYYMMDD
format. Is there an easier way than concatenating Date.getYear()
, Date.getMonth()
, and Date.getDay()
?
推荐答案
修改了我经常使用的一段代码:
Altered piece of code I often use:
Date.prototype.yyyymmdd = function() {
var mm = this.getMonth() + 1; // getMonth() is zero-based
var dd = this.getDate();
return [this.getFullYear(),
(mm>9 ? '' : '0') + mm,
(dd>9 ? '' : '0') + dd
].join('');
};
var date = new Date();
date.yyyymmdd();
这篇关于从 JS 日期对象获取 YYYYMMDD 格式的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文