4

My current date is Mon Jul 03 2017 10:17:40 GMT+0530 (India Standard Time).

I want to convert this date into a specific timezone and locale, depending on the user, for example, to GMT+00:00.

How can I convert a given date to a user-specific format?

Etheryte
  • 24,589
  • 11
  • 71
  • 116
krish
  • 79
  • 1
  • 8
  • 4
    See [moment.js](http://momentjs.com/). – Etheryte Sep 18 '17 at 10:53
  • Yes I am using moment.js. i.e `moment(d).format(format)` But how can I convert it in to the user specific time zone format. Destination format is not fixed in my case. – krish Sep 18 '17 at 10:58
  • @krish—you don't need to do anything to convert a Date to the user's timezone, the built-in *Date.prototype.toString* will use the host settings to generate a suitable string. – RobG Sep 19 '17 at 04:32

3 Answers3

2

You can use Moment.js' localized formats to show the date depending on the locale.
To detect the current user locale, see this question.

Note that you have to either load locales manually or use the release build with locales built in (moment-with-locales.js on the homepage).

Given your above example:

const locale = window.navigator.userLanguage || window.navigator.language
moment.locale(locale)
console.log('locale: ' + moment.locale())

const format = 'LLLL ZZ'
console.log(moment().format(format))
<script src="https://cdn.rawgit.com/moment/moment/b8a7fc31/min/moment-with-locales.min.js"></script>
Etheryte
  • 24,589
  • 11
  • 71
  • 116
1

There are many sample in Momentjs timezone Docs.

moment.tz('America/Los_Angeles').format('z')  // "PDT"     (abbreviation)
moment.tz('Asia/Magadan').format('z')         // "+11"     (3-char offset)
moment.tz('Asia/Colombo').format('z')         // "+0530"   (5-char offset)

Link :

https://momentjs.com/timezone/docs/

hsyn.ozkara
  • 117
  • 2
  • 11
-1

You can try this

var date = new Date();
//getTimezoneOffset() gives difference in minutes between your timezone and GMT, multiplying it by 60*1000 converts it to milliseconds
var utc_time = new Date(date.valueOf() + date.getTimezoneOffset() * 60000);
var offset = destination_timezone_offset_in_milliseconds;
var time_in_required_timezone = new Date(utc_time.valueOf()+offset);
Shriyansh Gautam
  • 1,084
  • 1
  • 7
  • 13
  • Destination time zone format is not fixed.So, cant use this solution.Thanks – krish Sep 18 '17 at 11:03
  • where does this javascript runs? on your machine or users machine? @krish – Shriyansh Gautam Sep 18 '17 at 11:18
  • Different machines and different locations.@Shriyansh Gautam – krish Sep 18 '17 at 11:20
  • means users sitting in different location, but for each user this code will return offset in their timezone by their machine – Shriyansh Gautam Sep 18 '17 at 11:22
  • so it works if the locations are different.But is there any way to change the date based on time zone on single machine. – krish Sep 18 '17 at 11:28
  • see the updated answer for time in specified timezone on single machine – Shriyansh Gautam Sep 18 '17 at 11:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154679/discussion-between-krish-and-shriyansh-gautam). – krish Sep 18 '17 at 11:37
  • Code without an explanation of how it addresses the OP is not helpful. Creating multiple Date objects is unnecessary. This code could do everything by adjusting the UTC time value of the original *date* by the required offset (in convenient units), then getting UTC time values. – RobG Sep 19 '17 at 04:36