HTML input date is being read back "incorrectly" [duplicate] - javascript

This question already has an answer here:
Initialize JS date object from HTML date picker: incorrect date returned
(1 answer)
Closed 3 years ago.
When I take in 3/15/2019 as the input date and then read back the time with console.log(fromDate) I get Thu Mar 14 2019 19:00:00 GMT-0500 (Central Daylight Time).
Why is it not Fri Mar 15 2019 00:00:00 ?
Is it because of the time on my local computer?
Is there a way to achieve the desired output?
fromDate = new Date(document.getElementById("fromDate").value);

This happens because new Date consider the date as UTC, so
var date = new Date('3/15/2019')
creates the date Fri Mar 15 2019 00:00:00. When you print the string however it considers your actual timezone, which is GTM-0500, so it removes 5 hours from the original date, yielding Thu Mar 14 2019 19:00:00.

Related

Can you create a dateString from returned values from getFullYear, getMonth, getDate [duplicate]

This question already has answers here:
Javascript date format like ISO but local
(12 answers)
How do I output an ISO 8601 formatted string in JavaScript?
(16 answers)
Closed 4 months ago.
I have a date like this
Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)
I am running this function on this date
const date = Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)
console.log(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())
Which returns
2022 9 29 2 0 0
Is it possible to convert these numbers to
2022-10-29T02:00:00.000
the whole point of this is to use a library like moment-timezone and parse 2022-10-29T02:00:00.000 to a new timezone for example new_york. Im doing this to get the UTC time for 2022-10-29T02:00:00.000 in new york. The issue with new Date(2022-10-29T02:00:00.000) is that it always converts it to my local timezone.

Get timestamp with milliseconds in Javascript [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
I want to get the current date including the milliseconds. I know that Date.now() is returning the Unix epoch time in milliseconds, but the object Date contains only the Date and timestamp with second precision.
console.log("Time: " + new Date);
Time: Tue Apr 14 2020 12:21:26 GMT+0200 (Central European Summer Time)
But I want it to look like this:
Time: Tue Apr 14 2020 12:21:26.500 GMT+0200 (Central European Summer Time)
Is there a way to change the format in order to display the milliseconds as well or do I need to concatenate the Date string with new Date().getMilliseconds();?
You can use moment.js to achieve the format you need, and the other option is to concatenate manually
console.log(moment().format('YYYY-MM-DD HH:mm:ss.SSS'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment-with-locales.min.js"></script>

How can I format date to string in javascript? [duplicate]

This question already has answers here:
Javascript - reformat date string to ISO8601
(2 answers)
Closed 3 years ago.
How can i format date in JS given in this format: Mon Jan 08 1996 00:00:00 GMT+0100 to string like this: "1996-01-06T23:00:00.000Z" ?
var str = "Mon Jan 08 1996 00:00:00 GMT+0100";
var date = new Date(str);
console.log(date.toISOString());
Make the string a date object, then convert it to ISO format. That's all there is.
use the following :
var event = new Date('Mon Jan 08 1996 00:00:00 GMT+0100');
console.log(event.toISOString());
First convert Mon Jan 08 1996 00:00:00 GMT+0100 to Date using new Date() function.
Second convert the new date into ISO format using toISO() function.

Getting `toISOString` string with 'GMT` Details [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 3 years ago.
I am looking for a date output like : "2019-05-29T06:33:33.537+05:30" - which includes gmt value. I tried to get the same by using:
var newFormat = new Date();
console.log(newFormat.toISOString());
But getting value as : 2019-06-16T03:00:27.989Z -not finds the GMT value added. What is the correct approach to get the required value?
Thanks in advance.
You can use toString():
var newFormat = new Date();
console.log(newFormat.toString());
toString gives you Wed Jan 23 2019 17:23:42 GMT+0800 (Singapore
Standard Time).
toDateString gives you Wed Jan 23 2019.
toLocaleString gives you 23/01/2019, 17:23:42.
toLocaleDateString gives you 23/01/2019.
toGMTString gives you Wed, 23 Jan 2019 09:23:42 GMT.
toUTCString gives you Wed, 23 Jan 2019 09:23:42 GMT.
toISOString gives you 2019-01-23T09:23:42.079Z.

Why does Javascript convert times differently? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
These are my two codes:
var date1 = new Date('2017-04-23');
var date2 = new Date('April 23, 2017');
console.log(date1);
console.log(date2);
this is the results:
Sat Apr 22 2017 17:00:00 GMT-0700 (PDT)
Sun Apr 23 2017 00:00:00 GMT-0700 (PDT)
why is date1 showing as the 22nd at 17:00?
JavaScript's Date parsing behavior is somewhat unreliable. It seems that when you give it an ISO 8601 string such as `"2017-04-23" it interprets the date as being in your own timezone, but when you give it an arbitrary string, it will interpret it as a UTC date.
Since you are in the GMT-7 timezone, the 22nd at 17:00 is the 23rd at 00:00 in UTC, and when you print out a date object, it will always print out the UTC date and not the localized date.
So, in summary, both dates are getting set to the 23rd at 00:00, but in different timezones. The first is being set to Apr 23 00:00 UTC-7 and the second one is being set to Apr 23 00:00 UTC.
It might be a good idea to always explicitly set a timezone in order to avoid this ambiguity.

Categories

Resources