jQuery将日期转换为唯一格式 [英] jQuery converting date to unique format

查看:83
本文介绍了jQuery将日期转换为唯一格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用jquery将日期转换为一种唯一的格式- 使用以下功能(也可以与Google的其他人一起尝试)

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate() ,
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}
document.getElementById('printdate').innerHTML =  formatDate('07/26/2017') ;

我有两种输入日期格式,例如- 1)5/16/2017和2)2017-5-16

预期输出为2017-07-16

对于印度时区,上述函数按预期的方式返回输出,如- 2017-07-16上述每个输入日期.

但是,如果我将时区"更改为美国/加拿大",则返回的日期是前一天,即 对于输入日期2017-06-26,它将返回2017-06-25,并且 输入日期07/26/2017,它返回2017-07-26

这是jsfidle- https://jsfiddle.net/L1j8ho1j/2/

原因是什么,它要在日期前一天返回?

还有其他函数可以转换日期,并以预期的格式返回相同的输入日期吗?

感谢您的帮助.

谢谢.

解决方案

如果您愿意使用 Moment.js ,那么您可以使用 moment-timezone .这可以解决您的日期问题时区和日期格式.

您需要加载以下所有内容:

使用它的formatDate方法应如下所示.

function formatDate(time, zone) {
    var format = 'YYYY-MM-DD';
    return moment(time, format).tz(zone).format(format);
}

moment.tz.add("Asia/Kolkata|HMT +0630 IST|-5R.k -6u -5u|01212|-18LFR.k 1unn.k HB0 7zX0|15e6")
moment.tz.add("America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0")

var date = new Date();
console.log(formatDate(date, 'Asia/Kolkata'));
console.log(formatDate(date, 'America/Los_Angeles'));

您可以通过链接.

Trying to convert date in one unique format with jquery- Using below function (also, tried with others from google)

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate() ,
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}
document.getElementById('printdate').innerHTML =  formatDate('07/26/2017') ;

I am having two date formats for input like - 1) 5/16/2017 and 2) 2017-5-16

Expected output would be 2017-07-16

For India Timezone, above function works as expected returning output like - 2017-07-16 for each of above input dates.

But, If I change Timezone to US/Canada, it returning date one day before i.e. for input date 2017-06-26, it returns 2017-06-25 and for input date 07/26/2017, it returns 2017-07-26

Here is jsfidle - https://jsfiddle.net/L1j8ho1j/2/

What is reason, it is returning one day before date?

Is there any other function to convert date, returning same input date with expected formatting?

Any help is appreciated.

Thanks.

解决方案

If you are open to using Moment.js then you can use moment-timezone .This can solve your issues with date timezones and date formatting.

You need to load all of the following:

Using it your formatDate method should look something like this.

function formatDate(time, zone) {
    var format = 'YYYY-MM-DD';
    return moment(time, format).tz(zone).format(format);
}

moment.tz.add("Asia/Kolkata|HMT +0630 IST|-5R.k -6u -5u|01212|-18LFR.k 1unn.k HB0 7zX0|15e6")
moment.tz.add("America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0")

var date = new Date();
console.log(formatDate(date, 'Asia/Kolkata'));
console.log(formatDate(date, 'America/Los_Angeles'));

You can get latest timezone data via this link.

这篇关于jQuery将日期转换为唯一格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆