Date format in JS [duplicate] - javascript

This question already has answers here:
How do I output an ISO 8601 formatted string in JavaScript?
(16 answers)
Closed 4 years ago.
When I do new Date() in JS I get:
Thu Jul 26 2018 08:09:57 GMT+0200 (Central European Summer Time)
How can I get it in this format along with the included Z at the end?
2016-05-26t16:53:22.313Z

Though there are other answers available, I recently found that you can call toJSON() on the date object to get the ISO formatted string:
console.log((new Date()).toJSON());

You can use toISOString to get this:
The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero UTC offset, as denoted by the suffix "Z".
console.log(new Date().toISOString());

Related

Parse locale string to date [duplicate]

This question already has answers here:
How to set locale for parsing in moment.js?
(2 answers)
Javascript Date Parse with specific locale
(1 answer)
Closed 1 year ago.
I need a locale-based parser for string to Date. I have explored for solutions (both library or custom) but all I found are either assuming a fixed locale, or Date -> string which is fairly easier.
Example:
parse('1/2/2021', 'en-US') // must print Date object as "Sat Jan 02 2021 00:00:00 GMT+0530"
parse('1/2/2021', 'en-IN') // must print Date object as "Mon Feb 01 2021 00:00:00 GMT+0530"
Note that depending on the locale argument, 1 is parsed as a month (for US locale) or as day (for IN locale)

Get Hour from Date String in Timezone of Date String, Not Converted to Current Time

I have date strings like Sun May 10 2020 13:00:00 GMT-0800 (Pacific Standard Time) and 2020/05/10 13:00:00. I would like to retrieve the hour from these string in 24 hour format (in this case it would be, 13).
I have tried something like new Date('Sun May 10 2020 13:00:00 GMT-0800 (Pacific Standard Time)').getHours() but that first converts the hour to system's current timezone and then returns the hour. Instead, I need to be able to retrieve the literal hour from variously formatted date strings such as the two above examples.
Edit: Obviously the above is not the correct approach, what I'm asking is, is there a universal way to parse the hour out of a date string, whether it be the above two examples or others, and without regex?

How to covert Timestap 'String' to Date in javascript? [duplicate]

This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 4 years ago.
In javascript, I have a timestamp value as a string. How to get Date from the same.
I know we can get a date from timestamp like below:
console.log(new Date(1535704620000));
Fri Aug 31 2018 14:07:00 GMT+0530 (India Standard Time)
But in my case, I want to get the value from the Timestamp value in the string as below:
console.log(new Date('1535704620000'));
Invalid Date
Thank you in advance.
All you have to do is to turn the string passed into the Date constructor into a number, which can be done with the unary plus operator:
new Date(+'1535704620000')

why new Date("Tue Apr 08 2008 00:00:00 GMT+0530").getMonth() is returning 3 [duplicate]

This question already has answers here:
why does javascript getMonth count from 0 and getDate count from 1?
(4 answers)
getMonth in javascript gives previous month
(6 answers)
Closed 5 years ago.
new Date("Tue Apr 08 2008 00:00:00 GMT+0530").getMonth() returns 3?
Why does this datetime format returns -1 month. Also can any one please tell me is this UTC timestamp or GMT or ISO. I am pretty confused. Any help is greatly appreciated.
The month field is 0 indexed :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
month
Integer value representing the month, beginning with 0 for
January to 11 for December.
getMonth() is returning the right date, it's just zero indexed, so Jan = 0, Dec = 11
new Date("Tue Apr 08 2008 00:00:00 GMT+0530").getMonth()
Also, ISO formatted dates look like this:
YYYY-MM-DDTHH:mm:ss.sssZ
If the Z is present, UTC is used, otherwise you can replace the Z with +hh:mm or -hh:mm to specify an offset from UTC.
Possibly because it is returning the month index from base 0.
January=0
Febuary=1
March=2
April =3
etc.
Just a guess though.

How to convert the given date to dd-mm-yy format in javascript? [duplicate]

This question already has answers here:
Javascript change date into format of (dd/mm/yyyy) [duplicate]
(2 answers)
Closed 5 years ago.
How to convert the given date to format.
Input: Sun, 11 Jun 2017 14:14:37 GMT
Output: 11-06-17 DD-MM-YY
I have tried split and then the mapping of month text to month number.
You can do it like this:
var input = "Sun, 11 Jun 2017 14:14:37 GMT";
console.log(new Date(input).toJSON().replace(/^.*(\d\d)-(\d\d)-(\d\d).*$/, '$3-$2-$1'));

Categories