简体   繁体   English

将UTC时间转换为yyyymmdd格式的Javascript

[英]Convert UTC time to yyyymmdd format Javascript

I'm trying to convert a date object to UTC and then convert it to yyyymmdd format. 我正在尝试将日期对象转换为UTC,然后将其转换为yyyymmdd格式。 Here is my code 这是我的代码

 var scheduleDate = '2016/10/29 17:00'; var d = new Date(scheduleDate); var utcdate = d.toUTCString(); var mm = utcdate.getUTCMonth() + 1; var dd = utcdate.getUTCDate(); alert( [date.getUTCFullYear(), !mm[1] && '0', mm, !dd[1] && '0', dd].join('')); 

But I'm getting the below error: 但是我收到以下错误:

utcdate.getUTCMonth is not a function

How can i get the month and date from a UTC string date format ? 如何从UTC字符串日期格式获取月份和日期?

Your variable utcdate is a string and so it does not have getUTCMonth or getUTCDate methods. 您的变量utcdate是一个字符串,因此它没有getUTCMonthgetUTCDate方法。

Further down you call the getUTCFullYear method on a variable that is not defined: date . 再往下,对getUTCFullYear的变量date调用getUTCFullYear方法。

Finally, the way you try to pad with zeroes will not work, as mm and dd are not strings, and so when you index them with [1] , JavaScript will coerce these numbers to Number objects, but they don't have a 1 property, even when they have two digits. 最后,您尝试用零填充的方法将不起作用,因为mmdd都不是字符串,因此当您使用[1]对其进行索引时,JavaScript会将这些数字强制为Number对象,但它们没有1属性,即使它们有两位数字也是如此。 So you'll always get the padded 0 on top of that. 因此,您总会得到填充的0

If you would fix that by first coercing those numbers to strings, it still would not work, as the && operator will yield false in some cases, which will be output as such: false . 如果您要通过首先将这些数字强制转换为字符串来解决该问题,那么它仍然无法正常工作,因为&&运算符在某些情况下会产生false ,将其输出为: false

Long story short, there are easier ways to get the UTC yyyymmdd format of a date. 长话短说,有更简单的方法来获取日期的UTC yyyymmdd格式。 For example by extracting it from the left side of the yyyy-mm-ddThh:nn:ss..... format that toISOString returns: 例如,从toISOString返回的yyyy-mm-ddThh:nn:ss.....格式的左侧提取它:

 var d = new Date('2016/10/29 17:00'); console.log(d.toISOString().replace(/-|T.*/g,'')); 

Be aware that the UTC date is at some moments in time one day off compared to the date in the local time zone. 请注意,与当地时区的日期相比,UTC日期在一天中的某些时间有所偏移。 If you need to prevent that, then I would suggest using toLocaleDateString with a locale that has a big-endian date format standard , such as Sweden, Lithuania, Hungary, South Korea, ...: 如果需要防止这种情况,那么我建议将toLocaleDateString与具有大尾数日期格式标准的语言环境一起使用,例如瑞典,立陶宛,匈牙利,韩国,...:

date.toLocaleDateString('se')

To remove the delimiters ( - ) is just a matter of replacing the non-digits: 删除定界符( - )只是替换非数字的问题:

 var d = new Date('2016/10/29 17:00'); console.log(d.toLocaleDateString('se').replace(/\\D/g, '')); 

If the known date is for UTC time zone, you can create the date object using the Date.UTC() method, then you can display the date local to the client (browser) time zone like so: 如果已知日期是UTC时区,则可以使用Date.UTC()方法创建日期对象,然后可以显示客户端(浏览器)时区的本地日期,如下所示:

 //var scheduleDate = '2016/10/29 17:00'; // If known date is in UTC, create the date object like so: var d = new Date(Date.UTC(2016,(10 - 1),29,17,0)); // UTC version of the date var mmUTC = d.getUTCMonth() + 1; var ddUTC = d.getUTCDate(); var hhUTC = d.getUTCHours(); // local to the browser (client) time zone var mm = d.getMonth() + 1; var dd = d.getDate(); var hh = d.getHours(); // display values console.log('UTC values: hour:['+ hhUTC +'] month:['+ mmUTC +'] day:['+ ddUTC +']'); console.log('Client Time: hour:['+ hh +'] month:['+ mm +'] day:['+ dd +']'); console.log('Client yyyymmdd:['+ d.getFullYear() + ((mm < 10) ? '0'+ mm : mm) + dd +']'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM