Angular JS - Date changes when submitting to $http - Timezone issue - javascript

I am having a strange problem where a Date changes when it is passed to API through $http.put, I suspect a timezone issue:
Datepicker triggers ng-change event - console.log:
Tue Jun 10 2014 00:00:00 GMT+0100 (GMT Summer Time)
Passed to API using Angular $http.put...
When it hits Fiddler:
StartDate=2014-06-09T23:00:00.000Z
As you can see the date changes from 10th June to 9th June.
How can I stop this change of date? Is it the timezone causing the change? Both the API and client side are running on Localhost.
Further to this:
When the field is clicked a second time and the datepicker launched / date selected, this second time the problem does not appear:
Console.log:
Wed Aug 06 2014 22:00:00 GMT+0100 (GMT Summer Time)
Fiddler data received:
StartDate=2014-08-06T21:00:00.000Z

I think it is a TZ issue, b/c the difference between your GMT+0100 and your StartDate=2014-06-09T23:00:00.000Z is 5 hours.
.
The issue:
Your local time is currently BST (British Summer Time) equivalent to GMT +1
This is going to be the default time when you make your API call.
However, the API was written by Google in California, and they are rather egocentric. Therefore, they're automatically converting the time since you're not providing any date formatting "instructions".
In other words, Chrome does the conversion for you nicely when you print to the console.log(), but when you make your $http.put, Angular, without explicit formatting for you, makes the call using it's default TZ (which is PST)
.
Resolution
You need to force the date formatting to your locale.
Angular template
{{ date_expression | date : 'yyyy-MM-dd HH:mm:ss'}}
In JavaScript
$filter('date')(date, 'yyyy-MM-dd HH:mm:ss')
using localization
<html>
<head>
<title>My Angular page</title>
<script src="angular-locale_en-uk.js"></script>
...
</head>
<body>
...
</body>
</html>

Related

Date in Javascript and Java(Spring boot)

I've read many posts on this topic, still i have some questions
first i get an date input (28/09/2019) from html input tag, let's call it aDate
<input name="ExpireDate" ng-model="personalDetail.ExpireDate" type="date">
in Javascript
console.log(aDate) gives Sat Sep 28 2019 00:00:00 GMT+1000 (Australian Eastern Standard Time)
console.log(aDate.toISOString()) gives 2019-09-27T14:00:00.000Z
i know they are both correct as the first one is UTC+10 time and the second one is UTC+0 time
and then i pass aDate to a spring boot application, it shows 2019-09-27T14:00:00.000Z if i parse it to LocalDate(which is supported by postgresql) it will lose T14:00:00.000Z part
then if i try to convert LocalDate 2019-09-27 in Java to Date in Javascript the date is now one day off.
my current solution is use LocalDateTime instead of LocalDate, it worked but i really dont want to store that time info in database
is there a way to get rid of the time info in Javascript Date? or any other solutions to tackle this
another strange things is why i specified type="date" in html input tag, it still give me datetime data?
UPDATE:
LocalDateTime does not work
converting 2019-09-27T14:00:00.000Z to LocalDateTime yields 2019-09-27T14:00
but in Javascript new Date('2019-09-27T14:00') treats it as local time and yields
"Fri Sep 27 2019 14:00:00 GMT+1000 (Australian Eastern Standard Time)"
"2019-09-27T04:00:00.000Z"

convert date string to ISO date with timezone

I have a collection of dates and times, it is formatted like this:
01.07.2013 16:10.
I know I have to rearrange to match ISO standard, so I managed to end up with a string like this:
2013-07-01T16:10.
From this, I need to create a Date Object. After reading many questions on here about this I am a little confused about how to add a timezone to this. All of the times are in New York Cities local time, which I also want to store the dates in.
So since Javascript takes the timezone from my machine, I changed it to be the one from NYC. However, the following part confuses me the most:
> print(new Date("2013-07-01T16:10"))
Mon Jul 01 2013 16:10:00 GMT+2000 (EDT)
Question: Why does it say GMT+2000 when my local time is GMT-0400 (the one of NYC)? Is this 'correct'? If not, how is this done properly?
--Updates:
I am using mongoDB 3.4.4 and interpreter Version is MozJS-38. The systemsetup -gettimezone returns America/New_York. Running mongo-express in chrome shows: Mon Jul 01 2013 06:00:00 GMT+0200 (EDT) for ISODate("2013-07-01T04:00:00.000Z") stored, but the shell says Tue Jul 02 2013 00:00:00 GMT+2000 (EDT) for print(new Date("2013-07-01T04:00:00.000Z"));.
You didn't specify the time zone offset when creating the Date object.
> new Date("2013-07-01T16:10-04:00")
2013-07-01T20:10:00.000Z

How do I change the time zone in JavaScript?

I've been working on a time app for JS and I was wondering, how could I change the time zone. See, I live in the eastern time zone of North America, and i don't like converting it to the International Timezone. So here is my Code.
<button type="button"
onclick="document.getElementById('time').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="time"></p>
When I click the button, I get,
Wed Dec 30 2015 13:40:25 GMT+0000 (UTC)
But I want it to be:
Wed Dec 30 2015 8:40:25 (EST)
Date.prototype.toString is supposed to give you a string in your local time (as configured on your client).
When you click Run code snippet this should show an alert dialog with your local time string (toString is implicitly called, like in your example);
alert(new Date());
If it does give you the expected result then either you client timezone is not configured correctly or the browser you are using is bugged.

Javascript date formatting - one hour out due to daylight saving

So now, its 9:23am. I have a UTC date string that represents the current date, that looks like this "2012-07-17T09:23:27.75"
I want that in a date object, so I can display a nicely formatted date, so I:
var myDate = new Date("2012-07-17T09:23:27.75")
// Gives --> Tue Jul 17 2012 10:23:27 GMT+0100 (GMT Daylight Time)
So because of daylight saving time I'm getting an hour-out issue. I can see that myDate.getTimezoneOffset() gives me -60, what's the standard / best practice way to get my date to actually reflect the current correct time? Have I just entered javascript date hell?
Try momentjs.com. I really found it handy for such things.
var myDate = moment("2012-07-17T09:23:27.75");
Gives you a date instance in your timezone (that basically configured on your computer). Moreover momentjs has nice human friendly formattings like "a couple of seconds ago", "a month ago",...
Dates are really a hell in JS (but not only in JS). The best thing you can do is to always only transport in UTC between browser <-> server. Then on the server convert it to what time format you like, you obviously only have to be consistent. That way I managed to handle date-times properly.
Try removing the 'T'
I was debugging some date time format issue in chrome when I found out that in console
new Date('2016-04-16T15:15:00') returns Sat Apr 16 2016 16:15:00 GMT+0100 (GMT Daylight Time)
while
new Date('2016-04-16 15:15:00') returns Sat Apr 16 2016 15:15:00 GMT+0100 (GMT Daylight Time)

jQuery.datepicker.formatDate and timezone offset

To handle dates, i'm using a jQuery UI public method in my application: jQuery.datepicker.formatDate
See params & source here : https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.datepicker.js
However the wrong date is displayed sometimes, according to the computer timezone.
Demo here : http://jsfiddle.net/7ACdB/
With a UTC+1 (paris) timezone in windows, i got :
03/30/20
03/30/20
With a UTC-6 (us&canada) timezone in windows, i got :
03/29/20 <- meh!
03/30/20
You need to restart your browser (well for google chrome at least) when you change the OS timezone.
My problem is the "03/29/20" date as you can imagine.
Can somebody explains to me if this is normal or a jquery ui issue ?
I'm beginning to think that it is normal to see a "Mon Mar 30 2020 00:00:00 GMT+0200 (Romance Daylight Time)" as 03/29/20 in a US timezone but i'm not so sure. :-/
What you're getting is correct. Your example sets the time at midnight for Paris. Midnight in Paris is 6PM the day before in the US for the Eastern Time Zone which I am in.
Your first time: GMT+0200 is Paris
Your second time: GMT-0500 is Chicago (note it is currently Daylight Savings Time)
So, when you change the time zone to US (using one of our 6 time zones), the output is the time in the US when is that time in Paris.
Here is an updated fiddle with a the time set to 6 AM Paris: http://jsfiddle.net/jensbits/7ACdB/1/

Categories

Resources