RollADie
RollADie

Reputation: 179

get time in a specified timezone using moment.js

Thu Apr 06 2018 09:24:00 GMT+0530 (India Standard Time)

I want to get this date in ISO format.(Not just in IST timezone, but other timezones as well). If I use .toISOString() method of Date it converts the date to UTC. I want to get the date in ISO format but retain the date in the given timezone. How can I do that?

Upvotes: 2

Views: 3908

Answers (1)

Nirali
Nirali

Reputation: 1786

Check below snippet to convet into different timezone

and refer to get date in ISO format Refer this Link

function toTimeZone(time, zone) {
    var format = 'YYYY/MM/DD HH:mm:ss ZZ';
    return moment(time, format).tz(zone).format(format);
}

//Current Date
var CurrentDate = moment().toISOString();
console.log(CurrentDate);

/*var date = '2018/03/23 05:00:00 +0000';*/
var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

//Current Date in server timezone
var result = toTimeZone(CurrentDate, timezone);
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);
console.log(result);

var result = toTimeZone(result, 'America/Los_Angeles');
console.log('America/Los_Angeles');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 2

Related Questions