Javascript 从 JS 日期对象获取 YYYYMMDD 格式的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3066586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get String in YYYYMMDD format from JS date object?
提问by IVR Avenger
I'm trying to use JS to turn a date objectinto a string in YYYYMMDDformat. Is there an easier way than concatenating Date.getYear(), Date.getMonth(), and Date.getDay()?
我正在尝试使用 JS 将 adate object转换为YYYYMMDD格式的字符串。难道还有比串联更简单的方法Date.getYear(),Date.getMonth()和Date.getDay()?
回答by o-o
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();
回答by Pierre Guilbert
I didn't like adding to the prototype. An alternative would be:
我不喜欢添加到原型中。另一种选择是:
var rightNow = new Date();
var res = rightNow.toISOString().slice(0,10).replace(/-/g,"");
<!-- Next line is for code snippet output only -->
document.body.innerHTML += res;
回答by Ramzi SAYAGH
You can use the toISOStringfunction :
您可以使用该toISOString功能:
var today = new Date();
today.toISOString().substring(0, 10);
It will give you a "yyyy-mm-dd" format.
它会给你一个“yyyy-mm-dd”格式。
回答by H6.
回答by Brian Powell
This is a single line of code that you can use to create a YYYY-MM-DDstring of today's date.
这是一行代码,可用于创建YYYY-MM-DD今天的日期字符串。
var d = new Date().toISOString().slice(0,10);
回答by NLemay
If you don't need a pure JS solution, you can use jQuery UI to do the job like this :
如果您不需要纯 JS 解决方案,您可以使用 jQuery UI 来完成这样的工作:
$.datepicker.formatDate('yymmdd', new Date());
I usually don't like to import too much libraries. But jQuery UI is so useful, you will probably use it somewhere else in your project.
我通常不喜欢导入太多的库。但是 jQuery UI 非常有用,您可能会在项目的其他地方使用它。
Visit http://api.jqueryui.com/datepicker/for more examples
回答by gongqj
new Date('Jun 5 2016').
toLocaleString('en-us', {year: 'numeric', month: '2-digit', day: '2-digit'}).
replace(/(\d+)\/(\d+)\/(\d+)/, '--');
// => '2016-06-05'
回答by Eric Herlitz
In addition to o-o's answer I'd like to recommend separating logic operations from the return and put them as ternaries in the variables instead.
除了oo的答案之外,我还建议将逻辑操作与返回分开,并将它们作为三元组放在变量中。
Also, use concat()to ensure safe concatenation of variables
此外,用于concat()确保变量的安全连接
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear();
var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based
var dd = this.getDate() < 10 ? "0" + this.getDate() : this.getDate();
return "".concat(yyyy).concat(mm).concat(dd);
};
Date.prototype.yyyymmddhhmm = function() {
var yyyymmdd = this.yyyymmdd();
var hh = this.getHours() < 10 ? "0" + this.getHours() : this.getHours();
var min = this.getMinutes() < 10 ? "0" + this.getMinutes() : this.getMinutes();
return "".concat(yyyymmdd).concat(hh).concat(min);
};
Date.prototype.yyyymmddhhmmss = function() {
var yyyymmddhhmm = this.yyyymmddhhmm();
var ss = this.getSeconds() < 10 ? "0" + this.getSeconds() : this.getSeconds();
return "".concat(yyyymmddhhmm).concat(ss);
};
var d = new Date();
document.getElementById("a").innerHTML = d.yyyymmdd();
document.getElementById("b").innerHTML = d.yyyymmddhhmm();
document.getElementById("c").innerHTML = d.yyyymmddhhmmss();
<div>
yyyymmdd: <span id="a"></span>
</div>
<div>
yyyymmddhhmm: <span id="b"></span>
</div>
<div>
yyyymmddhhmmss: <span id="c"></span>
</div>
回答by Darren Griffith
I don't like modifying native objects, and I think multiplication is clearer than the string padding the accepted solution.
我不喜欢修改本机对象,我认为乘法比填充已接受解决方案的字符串更清晰。
function yyyymmdd(dateIn) {
var yyyy = dateIn.getFullYear();
var mm = dateIn.getMonth() + 1; // getMonth() is zero-based
var dd = dateIn.getDate();
return String(10000 * yyyy + 100 * mm + dd); // Leading zeros for mm and dd
}
var today = new Date();
console.log(yyyymmdd(today));
回答by Dormouse
Plain JS (ES5) solution without any possible date jump issues caused by Date.toISOString() printing in UTC:
纯 JS (ES5) 解决方案,没有任何可能由 Date.toISOString() 在 UTC 打印引起的日期跳转问题:
var now = new Date();
var todayUTC = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()));
return todayUTC.toISOString().slice(0, 10).replace(/-/g, '');
This in response to @weberste's comment on @Pierre Guilbert's answer.
这是对@weberste 对@Pierre Guilbert 回答的评论的回应。

