原因是什么,它要在日期前一天返回?还有其他函数可以转换日期,并以预期的格式返回相同的输入日期吗?感谢您的帮助.谢谢.解决方案如果您愿意使用 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-16Expected output would be 2017-07-16For 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 andfor input date 07/26/2017, it returns 2017-07-26Here 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:momentmoment-timezoneUsing 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将日期转换为唯一格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 14:12