Anjala Baby
Anjala Baby

Reputation: 63

Format Date based on Browser time zone (VueJs/JS)

I have a Date in string Format: 2020-07-13 7:07 AM (which is indian time). I need to change this time based on browser time zone which can be either in US or Africa.

I have tried following ways, but i am not able to convert it correctly. Attaching my steps:

var d = "2020-07-13 7:07 AM";
var date = new Date(d); //Mon Jul 13 2020 07:07:00 GMT+0530 (India StandardTime)
var date1 = moment(date, 'YYYY-MM-DD HH:mm T'); //undefined

Please help me out. I havee to this in both VueJs and Javascript

Upvotes: 0

Views: 8746

Answers (2)

Anjala Baby
Anjala Baby

Reputation: 63

I figure out the answer in this way:

    var buildDate = "2020-07-13_7:07";
    let releaseDate = buildDate .split('_');
    let date = moment(date);
    let newDate = moment(new Date(date.get('year'), date.get('month'), date.get('date'), date.get('hour'), date.get('minute'))).add(new Date().getTimezoneOffset()*-1, 'm').toDate();
let result = moment(newDate).format('MMM DD YYYY h:mm A');
        

                  

Upvotes: 0

whereo
whereo

Reputation: 190

You should convert your timestamps to an ISO 8601 format including the UTC-offset. This can be done for instance using new Date().toISOString(). Then you can feed the timestamp into moment or, if you want to display the time for a different timezone, have a look at moment-timezone

Upvotes: 1

Related Questions