0

one guy suggest this to me

If you know the UTC offset then you can pass it and get the time using following function:

function calcTime(city, offset) {
    // create Date object for current location
    var d = new Date();

    // convert to msec
    // subtract local time zone offset
    // get UTC time in msec
    var utc = d.getTime() - (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    var nd = new Date(utc + (3600000*offset));

    // return time as a string
    return "The local time for city"+ city +" is "+ nd.toLocaleString();
}

alert(calcTime('Bombay', '+5.5'));

but i know the user pc timezone like "Asia/Kolkata" or "Europe/London" then how could i fetch date and time using any js library. moment.js can do that if yes so please give me some moment.js related code where i will pass time zone id and get that timezone date & time. thanks

Mou
  • 15,673
  • 43
  • 156
  • 275
  • please have a look here http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript – Christos Dec 11 '14 at 15:13
  • can u suggest any link or article where people use moment.js library for getting date time specific to timezone like "Asia/Kolkata" or "Europe/London". – Mou Dec 11 '14 at 15:15
  • unfortunately, I have nothing I could suggest for moment.js – Christos Dec 11 '14 at 15:18
  • Use [moment-timezone](http://momentjs.com/timezone/). Also, the approach in your example code is a common mistake. You cannot fool JavaScript's `Date` object to use any time zone other than the local one. All you are doing in the above code is *shifting the moment in time*. You will get strange results near DST transitions if the local time zone has different DST rules than the offset you're passing in. – Matt Johnson-Pint Dec 11 '14 at 19:24
  • @MattJohnson: moment().tz("Europe/London").format(); this code will return london time? if yes then how to get only date or time like in hour:minute; please advise if u know. thanks for reply – Mou Dec 12 '14 at 07:30
  • @MattJohnson: i go to this site http://momentjs.com and saw 2 other js file called moment-timezone-2010-2020.min.js and moment-timezone-all-years.js but got no info like what purpose these 2 files used. if u know about that 2 file then please share the knowledge. thanks – Mou Dec 12 '14 at 09:32
  • @MattJohnson: one more question. i am using this library https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js to get browser timezone id but i like to know can we use momentjs to get browser timezone without giving any input rather just call a momentjs function which will return browser timezone? please give me the info if u aware. thanks – Mou Dec 12 '14 at 09:42
  • @Mou - Please use StackOverflow properly. Asking new questions in comments isn't helpful for anyone. The point of S.O. is to build a repository of useful questions and answers for others - *not* just to help you work through things. If you read the moment and moment-timezone docs, and you search before asking, you'll find these questions have been answered before. You can always ask new questions (as actual questions) if you don't find what you need. – Matt Johnson-Pint Dec 12 '14 at 18:16
  • @Mou - For your specific questions. 1) Read the moment docs for the `format` function. 2) include moment.js and *one* of the moment-timezone.* files (probably the 2010-2020 unless you have historical concerns). This is in the moment-timezone docs also. 3) jstimezonedetect will give you an ID you can use with moment-timezone. There is [a project here](https://github.com/Canop/tzdetect.js) that is working to build this into moment. I have no info on the status, so contact that author if you need more details. – Matt Johnson-Pint Dec 12 '14 at 18:35

0 Answers0