How to display this in format dd.mm.yyyy? [duplicate] - javascript

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
alert(end); //Wed Oct 22 2014 00:00:00 GMT+0200 (CEST)
How to display this in format dd.mm.yyyy ?

Use built-in methods on the Date object
alert(end.getDate() + "." + (end.getMonth() + 1) + "." + end.getFullYear());
Note: I'm adding 1 to the month, because javascript returns the month in the range 0-11, with January being 0, February 1, etc.
Additional Note: I put parenthesis around that addition, because otherwise it would just do another string concatenation, so July 6, 2013 would become 6-61-2013 without the parenthesis

If you don't mind the additional dependency, you could use Moment.js http://momentjs.com.

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)

How to format a date in JavaScript just as simply as in PHP in 2020? [duplicate]

This question already has answers here:
Format JavaScript date as yyyy-mm-dd
(50 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
Formatting a date takes 1 line of code in PHP but it takes so many lines in JavaScript, at least in old answers.
Is there a new way of going from a variable myDate containing "Mon Apr 20 2020 14:45:34 GMT+0100 (British Summer Time)" to "2020-04-20" in just 1 or 2 lines of code?
Format should be the same across locales so toLocaleDateString() isn't an option.

Date format in JS [duplicate]

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());

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