gpt4 book ai didi

javascript - 将日期格式转换为 'YYYYMMDD'

转载 作者:行者123 更新时间:2023-11-30 07:00:11 27 4
gpt4 key购买 nike

在 Javascript 中,我有如下所示的日期字符串:

var dateStr = "Wed Mar 25 2015 05:30:00 GMT+0530 (India Standard Time)";

我需要将其转换为“YYYYMMDD”格式。例如上面的日期应该是:"20150325"

最佳答案

我发现并一直使用它的一个很好的功能。

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();

关于javascript - 将日期格式转换为 'YYYYMMDD',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44943012/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com