Convert specific UTC timezone to local timezone - javascript

Okay, so I have been struggling with this problem for to many hours now. What I want to do is to convert "Brasilia Time (BRT) -0300 UTC" into user's local timezone. How can I do this using javascript? I've also tried some javascript libraries like moment.js but still no luck.

You do time.clone().tz('Yourlocalzone'). See also: http://momentjs.com/timezone/ for full documentation.

Related

What is the correct mapping of Mid-Atlantic timezone in momentjs?

What is the mapping of Mid-Atlantic timezone in momentjs? I have searched on stackoverflow for the correct mapping in momentjs but could not find a suitable answer.
If you're looking for a timezone identifier for UTC -2:00 to use with moment-timezone, try America/Noronha or Atlantic/South_Georgia

Converting time-stamp to milliseconds in javascript

I have a date which is of format 20170622T164322Z. How to convert it into milliseconds(epoch time) in javascript. I am new to javascript, kindly help
If you could use moment.js in your project, you can do this:
moment("20170622T164322Z").unix()
Example running on the console of the browser:
moment("20170622T164322Z").unix()
1498149802
Btw, moment.js is just great when dealing with date time.

JavaScript: How to set a date so that it looks the same all over the world?

One of our programmers decided to use a DATE field in the MySQL db in order to achieve this.
Sending and saving a JS date object did work well until the daylight saving changes intervened (with nasty effects :) ).
Of course, saving the date in a DATETIME field solves it, but everybody sees the time/dates in their own timezone.
We need everybody (all over the timezones) to see the same date!
I clarify this, to get the proper answers:
I want to keep using the DATE field storage type in MySQL (vs DATETIME - ok, maybe too much of an optimization, but it's already there and I want a long term solution for when I receive such structure/code from other developers)
Sending local time (local JS in browser) 23-05-2016, will reach the server as 22-05-2016 0X:X0:00Z (UTC) and be store as such. Because it's a DATE field, the stored value will become 22-05-2016 only. And you lost a day! :)
Our solution from bellow not only fixes the DATE field trimming, but also adds the fact that people now can see the same correct date (23-05-2016) no matter of the timezone they are in!
I like the outcome and would love to see some better solutions to achieve the same and improve the system.
Actually, we have noticed the problem only when the daylight saving time changed, so my solution (as answer bellow) is a good solution for that as well. And it only consumes resources client-side.
I have posted my own solution to this question as an answer bellow.
It would be really cool to see a much better solution from you!
With Javascript
Save your dates in ISO format (including timezone information) and use moment.js to convert the datetime to another timezone.
If moment.js is not already a dependency, and you want to avoid extra libraries, keep reading.
With MySQL
Instead of solving this problem when you write the data (losing timezone information in the process), solve it when you read the database.
In your SELECT query, normalize all DATETIME values to your preferred timezone using the convert_tz built-in function.
MomentJs is your best bet. Find the timezone you want and pass the ISO string to it and you should be good to go.
http://momentjs.com/timezone/docs/#/using-timezones/
A DATE is just a year, month, and day. It doesn't have a time, or a time zone. Think about your birthday or your wedding date, or today's date.
The JS Date object is not this at all. It's a timestamp. It's the number of milliseconds elapsed since Midnight January 1st 1970 UTC.
You should leave your date as a date-only wherever possible. Use the ISO-8601 date-only format, which is YYYY-MM-DD. If you have to assign it a time and time zone, then be very careful when you do.
If you just assign midnight local time, then you're risking losing a day (as you showed), and you're not considering that there are local days in some time zones where midnight does not exist! (Such as the spring-forward day in Brazil). Noon is a safer bet than Midnight, but still you should use this sparingly. The better approach is to keep dates as dates, not as date-times.
Also, I'd answer with code if I could, but you didn't provide any code in your question showing what was broken. Please read How do I ask a good question? and How to create a Minimal, Complete, and Verifiable example. Thanks.
There are more solutions to this, but the fastest and easiest that I could come up with is described bellow:
Let's intervene as early as possible in the information stream.
Just change the data before transmitting it through AJAX.
The function we used is this:
function addTimezoneDiffAnd12HoursToDate(date) {
var timezoneOffset = date.getTimezoneOffset();
date.setHours(12-Math.floor(timezoneOffset/60));
date.setMinutes(-timezoneOffset % 60);
return date;
}
What it does is that it converts a Date to be always at noon (12:00) UTC!
You can use it like this:
$scope.contract.contractDate = addTimezoneDiffAnd12HoursToDate($scope.contract.contractDate);
and send it as such to be stored in the DATE field.
Let me know if you have a simpler solution. I'd like to see it.

How to display the date example format " 15-Apr-2012 10.12 AM" exactly using this javascript new Date() format in javascript

I need to display the date format like this 15-Apr-2012 10.12 AM.I tried to use time=new Date() using javascript.But i couldnt get the correct date format .it shows the GMT global timing.I need to display the time format.please anyone can know?
Take a look at DateJS. If you're planning to do more stuff with dates than just formatting it, I would strongly suggest it. Definitely makes things easier with manipulation and displaying.
Easiest thing to do is use a library: try Moment, it is the real deal.

Javascript toUTCString hours off

I am getting a JSON string back from a webservice. I have tested this same thing on the W3C Schools site too for our local machine.
The time I am getting back for both the service and the W3C site from the UTC string via Javascript is 2 hours ahead. Any ideas how to fix this or do I need to adjust for daylight savings, or something else. Thanks in advance.
If you are living in a GMT-2 zone, you should get UTC time 2 hours ahead. If that's the case and you want to get local date, here is your answer.
Convert UTC Epoch to local date with javascript

Categories