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"
Related
I use ExcelJS to parse an Excel into a JavaScript Object.
Here is an excel sample :
As you can see, I must cover multiple format for date and hour. For the date I have no problem. But when it comes to hour, cells with "12:30" and "15:00" value are returned as javascript date object. So I check if its a date or a string and I want to get the string value (10H10 and 18h30 cells are ok because they are considered as string but not the 15:00 and 12:30).
When using VSCode debug tool, I can see that I have this date :
'Sat Dec 30 1899 12:39:21 GMT+0009 (Central European Standard Time)'
'Sat Dec 30 1899 15:09:21 GMT+0009 (Central European Standard Time)'
What I want is a way to cast this result in a string "12:30" or "15:30". I tried many things using moment and Date functions like .toUTCString(), .toGMTString()...
I'm going to assume you have a JS Date object.
Intl.DateTimeFormat would be recommended for formatting date and/or times for users.
But since you want a specific format, it might be easier to format it yourself.
But the time isn't in the correct time zone. You'd first have to convert it to the correct time zone. Except the correct time zone is UTC, and we can access that info about the components of the UTC representation of the date-time without doing any conversion.
[
dt.getUTCHours(),
dt.getUTCMinutes().toString().padStart(2, '0'),
].join(":")
Hello All, I have time in milliseconds and I want convert it into "dd/mm/yyyy hh:mm:ss"
I have tried the following code in javascript but I failed to do it, What I get is "Thu Jul 07 2016 16:22:10 GMT+0530 (IST)" this full timestamp, Code I tried is as follows:
self.users[i].lastLoggedIn = (new Date(self.users[i].lastLoggedIn)).toString("mm/dd/yyyy hh:mm:ss");
Where am I going wrong? for reference I have attached one screen shot of the same. Thank you..!!!
I suggest using moment.js for handling dates in Javascript. It is available for browsers, server-side engines and so on.
moment(self.users[i].lastLoggedIn).format("mm/dd/yyyy hh:mm:ss")
This question already has an answer here:
HTML Input type datetime-local setting the wrong time-zone
(1 answer)
Closed 7 years ago.
I'm writing a MVC 5 web application. Into this, I have multiple lists that I propagate and manage through javascript and jquery (one dataset, dependent select controls, and adding ajax callbacks would complicate it unnecessarily.)
The issue I have is: I have a hidden for field formatted to ISO 8601. I run into issues when I display the date in the user's local time, I get a shifted date.
So if the date were stated as: 01-01-2009 (in iso 8601 format: 2009-01-01), the user sees: 12-31-2008.
When I parse the date I'm using:
this.date = new Date(Date.parse(originalString));
/* ^- Date.parse is giving me a number. */
To display the text of the date I am using:
admin.date.toLocaleDateString().Concat(...
Do I need to do any sort of patch-up to adjust things to the proper time-zone? The date, when using console.log(admin.date); shows the original 2009-01-01
I'm thinking there's some parameter I'm not specifying correctly in the toLocaleDateString, but my familiarity level with it is low.
Edit: The goal is to prevent the date shift. All we store is the date, the time aspect is dropped. We have multiple time-zones posting to this database, and the goal is: We use the date of the person who posted it, time dropped. Were the date May 01, 2015, I want anyone who sees that date to see May 01, 2015, the 'toLocaleDateString' is merely a means to get it to appear format correct for their region. So someone who views dates as yyyy-mm-dd will see it properly.
Based on the documentation for Date.parse:
The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
You are getting back the epoch time in UTC for your parsed date string hence why the date is off for local times. So, you would need to know the time difference between the local time and UTC which Date.getTimezoneOffset provides (in minutes) in order to set the correct date for the local time:
> var date = new Date(Date.parse('2009-01-01'));
undefined
> date;
Wed Dec 31 2008 19:00:00 GMT-0500 (EST)
> date.getTimezoneOffset();
300
> date.setMinutes(date.getTimezoneOffset());
1230786000000
> date;
Thu Jan 01 2009 00:00:00 GMT-0500 (EST)
One thing to note though is:
...the offset is positive if the local timezone is behind UTC and negative if it is ahead.
So you might need to take care for locales where the value is negative if this applies to your application. If so maybe just omitting negative values would be enough since the date should be the same if a locale's timezone is ahead of midnight UTC.
EDIT: To compensate for possible issues with daylight savings time:
> var dateVals = String.prototype.split.call('2009-01-01', '-');
undefined
> var date = new Date(dateVals[0], dateVals[1] - 1, dateVals[2]);
undefined
> date
Thu Jan 01 2009 00:00:00 GMT-0500 (EST)
What is the best way in JavaScript to set the date portion of a JavaScript date object without affecting the time portion?
Lets say you have a date like this
Wed Oct 31 2012 10:15:00 GMT-0400 (EDT)
And you want to update it to be November 1st without changing the time.
At first glance, you'd think this would work,
exampledate.setFullYear(2012);
exampledate.setMonth(10);
exampledate.setDate(1);
But the result is
Sat Dec 01 2012 10:15:00 GMT-0500 (EST)
I think reason for this is that October has 31 days and November has just 30. Since this obviously doesn't work right all the time. What are my other options? Will I have to create a new date object?
JavaScript Date object setter methods often allow you to (optionally) set multiple date/time components all at once (in addition to the primary component), as appropriate for the situation --- this is what you want to do.
For your use case, the syntax for setFullYear is setFullYear(year,month,day), so in addition to explicitly setting the year (which would cause the month and date to be implicitly updated based on your initial date), you could also explicitly set the month and day while you're at it. For instance:
var d = new Date();
d.setFullYear(2020,10,3);
will return
Tue Nov 03 2020 13:10:34 GMT-0500 (EST)
See the JavaScript setFullYear() method for more details.
Because the time components of the Date object (e.g. hours, minutes, seconds, milliseconds) are not dependent on the date portions (year,month,date), your initial time components will be maintained. If you need to then update the time components, you can safely use setHours() or other methods as needed, because no implicit updates to the date components will occur.
First set the date, then the month.
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)