I have a string date and utcoffsetvar date = '06/30/2016';var utcOffset = -7.0;I need a Javascript function to convert string date to utc using utcoffsetI tried the following way it works only on chrome but doesn't work in IE/11/edge and FF. var date = '06/30/2016'; var utcOffset = -7.0; var startDate = moment(date).toDate(); var offsetDate = moment(startDate + offset).toDate();Is there any other way I could achieve this across all the browsers.I also tried the below approach but it doesn't workvar date = '06/30/2016'; var utcOffset = -7.0; var d = moment(date).toDate();d.setTime( d.getTime() + offset*60*1000 ); 解决方案 Please try the following function:function dateToUTC(date, offset) { var tzDate = moment(date).utcOffset(offset); var utcDate = new Date(date+tzDate.format('ZZ')); if(isNaN( utcDate.getTime() ) ) { return moment(date+tzDate.format('Z')) } return moment(utcDate);}Usage:var date = '06/30/2016';var utcOffset = -7.0;dateToUTC(date, utcOffset); // Thu Jun 30 2016 07:00:00 GMT+0000Demo: https://jsfiddle.net/3v9cd6uf/ 这篇关于将字符串日期转换为 UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-27 23:22