0

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

1
  • 1
    You need to include the timezone in your date string somehow, otherwise there's no way to localise it in any meaningful way. Commented Jul 17, 2020 at 8:18

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

10 Comments

I got date in 2020-07-13T01:37:00.000Z format. I have to take timezone based on browser time. Is it possible. I tried Intl.DateTimeFormat().resolvedOptions().timeZone, is it correct way to use in all browser?
To display timedata in the users own timezone, you can directly use moment in combination with the UTC-timestamp. However, if you want to store the users timezone or display the time for a different timezone, then you can grab the browsers timezone using Intl.DateTimeFormat().resolvedOptions().timeZone. All modern browsers support this option.
I have tried this, but getting wrong one:var jun = moment(dec);var t = jun.format(Intl.DateTimeFormat().resolvedOptions().timeZone); I am getting output as AM0iam/Cam7/13/2020cuttam
You cannot put the timezone into the format-function of moment. format() is purely for "how should the timestamp look like". To explicitly set the timezone for a timestamp you have to use an additional library like moment-timezone
I have given in this format, but getting error: var dec = moment("2020-07-13T01:37:00.000Z");var tt = moment.tz(dec, "Asia/Calcutta"); Whats wrong in this?? Showing error as moment-timezone.js:1 Uncaught TypeError: Cannot read property 'rule' of undefined at h.zoneAndRule (moment-timezone.js:1) at h.offset (moment-timezone.js:1) at Function.t.updateOffset (moment-timezone.js:1)
|
0

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');
        

                  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.