Javascript javascript日期到字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5914020/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:29:52  来源:igfitidea点击:

javascript date to string

javascriptstringdate

提问by Kiran

Here is what I need to do.

这是我需要做的。

Get Date, convert to string and pass it over to a third party utility. The response from the library will have date in string format as I passed it. So, I need to convert the date to string like 20110506105524 (YYYYMMDDHHMMSS)

获取日期,转换为字符串并将其传递给第三方实用程序。当我传递它时,来自库的响应将具有字符串格式的日期。所以,我需要将日期转换为字符串,如 20110506105524 (YYYYMMDDHHMMSS)

function printDate() {
    var temp = new Date();
    var dateStr = temp.getFullYear().toString() + 
                  temp.getMonth().toString() + 
                  temp.getDate().toString() +
                  temp.getHours().toString() + 
                  temp.getMinutes().toString() + 
                  temp.getSeconds().toString();

    debug (dateStr );
}

The problem with above is that for months 1-9, it prints one digit. How can I change it to print exactly 2 digits for month, date ...

上面的问题是,对于 1-9 个月,它会打印一位数字。如何更改它以精确打印月份、日期的 2 位数字...

回答by Alex K.

You will need to pad with "0" if its a single digit & note getMonthreturns 0..11 not 1..12

如果它是单个数字,则需要用“0”填充,注释getMonth返回 0..11 而不是 1..12

function printDate() {
    var temp = new Date();
    var dateStr = padStr(temp.getFullYear()) +
                  padStr(1 + temp.getMonth()) +
                  padStr(temp.getDate()) +
                  padStr(temp.getHours()) +
                  padStr(temp.getMinutes()) +
                  padStr(temp.getSeconds());
    debug (dateStr );
}

function padStr(i) {
    return (i < 10) ? "0" + i : "" + i;
}

回答by Stéphane

Relying on JQuery Datepicker, but it could be done easily:

依靠JQuery Datepicker,但它可以轻松完成:

var mydate = new Date();
$.datepicker.formatDate('yy-mm-dd', mydate);

回答by Tony Brix

use this polyfill https://github.com/UziTech/js-date-format

使用这个 polyfill https://github.com/UziTech/js-date-format

var d = new Date("1/1/2014 10:00 am");
d.format("DDDD 'the' DS 'of' MMMM YYYY h:mm TT");
//output: Wednesday the 1st of January 2014 10:00 AM

回答by Daniel Cerecedo

A little bit simpler using regex and toJSON().

使用正则表达式和toJSON().

var now = new Date();
var timeRegex = /^.*T(\d{2}):(\d{2}):(\d{2}).*$/
var dateRegex = /^(\d{4})-(\d{2})-(\d{2})T.*$/
var dateData = dateRegex.exec(now.toJSON());
var timeData = timeRegex.exec(now.toJSON());
var myFormat = dateData[1]+dateData[2]+dateData[3]+timeData[1]+timeData[2]+timeData[3]

Which at the time of writing gives you "20151111180924".

在写作的时候给你"20151111180924"

The good thing of using toJSON()is that everything comes already padded.

使用的好处toJSON()是一切都已经填充了。

回答by jammon

Maybe it is easier to convert the Date into the actual integer 20110506105524and then convert this into a string:

也许将 Date 转换为实际整数20110506105524然后将其转换为字符串更容易:

function printDate() {
    var temp = new Date();
    var dateInt =
        ((((temp.getFullYear() * 100 + 
            temp.getMonth() + 1) * 100 + 
           temp.getDate()) * 100 +
          temp.getHours()) * 100 + 
         temp.getMinutes()) * 100 + 
        temp.getSeconds();

    debug ( '' + dateInt );  // convert to String
}

When temp.getFullYear() < 1000the result will be one (or more) digits shorter.

temp.getFullYear() < 1000结果将缩短一位(或更多)位数时。

Caution: this wont work with millisecond precision (i.e. 17 digits) since Number.MAX_SAFE_INTEGER is 9007199254740991which is only 16 digits.

注意:这不会以毫秒精度(即 17 位)工作,因为 Number.MAX_SAFE_INTEGER9007199254740991只有 16 位。

回答by jammon

I like Daniel Cerecedo's answer using toJSON()and regex. An even simpler form would be:

我喜欢 Daniel Cerecedo 使用toJSON()和正则表达式的回答。更简单的形式是:

var now = new Date();
var regex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).*$/;
var token_array = regex.exec(now.toJSON());
// [ "2017-10-31T02:24:45.868Z", "2017", "10", "31", "02", "24", "45" ]
var myFormat = token_array.slice(1).join('');
// "20171031022445"