Skip to content Skip to sidebar Skip to footer

Convert String Date To Utc

I have a string date and utcoffset var date = '06/30/2016'; var utcOffset = -7.0; I need a Javascript function to convert string date to utc using utcoffset I tried the following

Solution 1:

Please try the following function:

functiondateToUTC(date, offset) {
    var tzDate = moment(date).utcOffset(offset);
    var utcDate = newDate(date+tzDate.format('ZZ'));
    if(isNaN( utcDate.getTime() ) ) {
        returnmoment(date+tzDate.format('Z'))
    }
    returnmoment(utcDate);
}

Usage:

var date = '06/30/2016';
var utcOffset = -7.0;

dateToUTC(date, utcOffset); // Thu Jun 30 2016 07:00:00 GMT+0000

Demo:https://jsfiddle.net/3v9cd6uf/

Solution 2:

Try this below code , it should work . It works well for me on all the browsers .

var d =newDate(modifieddate);
                var _userOffset =d.getTimezoneOffset()*60000; 
                d = newDate(d.getTime()+_userOffset);

You can put your own offset value instead of calculating . Let me know if that helped you :)

Post a Comment for "Convert String Date To Utc"