Define format for Date.parse in javascript [duplicate] - javascript

This question already has answers here:
different result for yyyy-mm-dd and yyyy/mm/dd in javascript when passed to "new Date" [duplicate]
(2 answers)
Closed 6 years ago.
I am using Date.parse to convert a string to a date in javascript, however, if the string looks like this '10/11/2016' it is interpreted as Oct 11 2016, i need it to be interpreted as Nov 10 2016
Suggestions?

By default Date.parse consider in this format that month precede the day to be in your case MM/DD/YYYY not as you want DD/MM/YYYY.
I prefer/suggest using 3rd party date parser library as Moment.js
It can take your date-string and the format to be like this:
moment("10/11/2016", "DD-MM-YYYY");

Related

Convert Date to javascript format [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I am trying to convert this date into Mon Nov 26 2018 10:32:04 GMT (I am getting this data from the Api so i can't make changes to it)
I assume it is considering 26 as months thats why it is showing it as invalid date
Can Anyone help me with this. How to convert that date into the expected output i specified.
How to get
var d = new Date("26-11-2018 10:32:04")
return d; //Error: Invalid Date
expected Output: Mon Nov 26 2018 10:32:04 (IST)
Use moment.js to parse the date.
moment("26-11-2018 10:32:04", "DD-MM-YYYY HH-mm-ss").toDate()
Alternatively, if you really don't want to use moment for whatever reason, you can use regex magic.
new Date("26-11-2018 10:32:04".replace(/^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/, "$3-$2-$1T$4:$5:$6Z"))
This is not a robust as #Yevgen answer but it also much simpler.
All I'm doing is removing the - and flipping the day and month values
const items = "26-11-2018 10:32:04".split('-')
new Date(`${items[1]} ${items[0]} ${items[2]}`)
This works for personal projects but I highly recommend using moment.js

Format date from DD.MM.YY format to DD/MM/YYYY format [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
How can I format date from DD.MM.YY format to any other format (preferably DD/MM/YYYY) with full 4 digits year value that I can input to new Date() by using javascript?
I find similar issue but in PHP code, is there an library that can convert that in javascript?
I'm getting Invalid Date error when inputting that date format to new Date()
Vanilla JavaScript can be used to convert the date string into a Date object by calling split(",") on the string, parseInt() on each returned array item, and then adding 1900 or 2000 to the year component based on your own needs. Date() takes the year, month, and day as arguments.
Once you have a JavaScript date object, a method like this using padStart() can output the date into the DD/MM/YYYY format.
See this example codepen: https://codepen.io/volleio/pen/BXWKyp

Is there a way to convert javascript Date object to string but retain the format as displayed when console.log is used on the Date object? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Create an ISO date object in javascript
(5 answers)
Closed 3 years ago.
I want to convert the date object to string but I want it in the same format as it is displayed when I do console.log(new Date()) which is something like '2019-05-21T11:55:39.496Z' and not the usual extended format we get when we do console.log(new Date().toString()) which is something like this 'Tue May 21 2019 17:28:51 GMT+0530 (India Standard Time)'.
console.log(new Date().toISOString())
2019-05-21T12:03:50.601Z

convert date format DD-MM-YYYY TO DD MMM YYYY format [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 5 years ago.
Is their any inbuilt method to display date as DD MMM YYYY (08 Jan 2018) format in javascript
Currently I am using the below code for date format.
$('#timeTable th:not(:first-child)').each(function(){
if($(this).attr('Id') === 'totalHours') return;
$(this).text(dateObj.getDate()+'/'+dateObj.getMonth()+'/'+dateObj.getFullYear());
dateObj.setDate(dateObj.getDate() + 1);
});
Nothing built in. Your approach is pretty much as good as you'll get without a library.
Note that months are zero indexed, so you need to add one to the result of getMonth.
EDIT
If your program doesn't need to run after the year 9999 you might get away with:
dateObj.toISOString().slice(0, 10);
toISOString produces a value like 2018-01-08T08:52:17.406Z. You can just use the first 10 characters.

Default year in Date if dateString does not have year part [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 6 years ago.
I was trying to check what values would return if partial values are sent.
var d = new Date("11/28");
console.log(d.toLocaleDateString())
I was expecting it to be 28th Nov., 2016(current year), but it returns 28th Nov., 2001.
So the question is Why is 2001 considered as default year?
2001 is not consider as default year.
This is a Chrome issue, if you run the same code with Firefox you get Invalid Date.
The language specification only includes rules for parsing ISO 8601 formatted strings. Parsing of any other format is implementation dependent (noting that the Date constructor and Date.parse are equivalent for parsing).
So given "11/28" is not a valid ISO 8601 string, the implementation is free to apply any heuristics it likes. Any result, including an invalid date, should be expected and consistency between implementations should not.

Categories

Resources