Convert Date to javascript format [duplicate] - javascript

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

Related

Convert String Day and Date to Date in Javascript [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Compare two dates with JavaScript
(44 answers)
Closed 5 years ago.
I have a date format:
Thursday, 10 Aug 2017
I have an array of dates like this above. I have to find out the earliest date in the the array in javascript.
How can I effectively parse the date in the above format to check which is the earliest date.
How can I do that?
I also tried Date.parse method. Its working. Is it a good way of doing this? Can this method break or throw some exceptions? Also will this add any added latency to the code? As this method checking is alll done client side and we don't want any latency of any kind.
Any leads appreciated.
You can use momentjs and parse the date with
moment.lang("en-au").format('LLLL');
Have a look at moment
If you do not want moment just use what you say above
var d = new Date('Thursday, 10 Aug 2017')
d.getDate() --- 10
d.getFullYear() --- 2017
And if you want to find the earliest one, you can do the following(it does need refactor):
var earliest = null;
var dates = ['Thursday, 11 Aug 2017', 'Thursday, 10 Aug 2017' , 'Thursday, 15 Aug 2017'];
dates.forEach(date => {earliest = earliest == null ? new Date(date) : (new Date(date).getTime() < earliest.getTime()) ? new Date(date) : earliest});
This is a duplicate, but you've asked many questions in one go:
How can I effectively parse the date in the above format to check which is the earliest date. How can I do that?
Duplicate of Why does Date.parse give incorrect results?
I also tried Date.parse method. Its working. Is it a good way of doing this?
No, for the reasons outlined in the duplicate: it gives unreliable results.
Can this method break or throw some exceptions?
No, but it gives unreliable results.
Also will this add any added latency to the code?
No, but it gives unreliable results.

JavaScript Date not working properly in IE8 [duplicate]

This question already has answers here:
Javascript JSON Date parse in IE7/IE8 returns NaN
(5 answers)
Closed 7 years ago.
Chrome showing result as expected but IE-8 giving NAN when i execute the following:
Chrome:
d = new Date("2014 12 01") // results Mon Dec 01 2014 00:00:00 GMT+0500 (Pakistan Standard Time)
IE-8:
d = new Date("2014 12 01") // results NaN undefined
The format you're trying to parse doesn't match the only specific format that new Date is required to parse. To parse it reliably cross-browser, you need to parse it explicitly — either in your own code, which can be trivially done with a regex, or using a library like MomentJS and telling it what the format is.
The trivial regex solution:
// NOTE! Uses local time.
var yourString = "2014 12 01";
var parts = yourString.match(/^(\d{4}) (\d{2}) (\d{2})$/);
if (parts) {
var date = new Date(+parts[1], +parts[2] - 1, +parts[3]);
alert(date.toString());
}

Javascript Date string constructing wrong date [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
Hi I am trying to construct a javascript date object with a string, but it keeps contructing the wrong day. It always constructs a day that is one day behind. Here is my code
var date = new Date('2006-05-17');
The date i want to get is
Wednesday May 17 2006 00:00:00 GMT-0700 (PDT)
But instead I get
Tue May 16 2006 17:00:00 GMT-0700 (PDT)
When you pass dates as a string, the implementation is browser specific. Most browsers interpret the dashes to mean that the time is in UTC. If you have a negative offset from UTC (which you do), it will appear on the previous local day.
If you want local dates, then try using slashes instead, like this:
var date = new Date('2006/05/17');
Of course, if you don't have to parse from a string, you can pass individual numeric parameters instead, just be aware that months are zero-based when passed numerically.
var date = new Date(2006,4,17);
However, if you have strings, and you want consistency in how those strings are parsed into dates, then use moment.js.
var m = moment('2006-05-17','YYYY-MM-DD');
m.format(); // or any of the other output functions
What actually happens is that the parser is interpreting your dashes as the START of an ISO-8601 string in the format "YYYY-MM-DDTHH:mm:ss.sssZ", which is in UTC time by default (hence the trailing 'Z').
You can produce such dates by using the "toISOString()" date function as well.
http://www.w3schools.com/jsref/jsref_toisostring.asp
In Chrome (doesn't work in IE 10-) if you add " 00:00" or " 00:00:00" to your date (no 'T'), then it wouldn't be UTC anymore, regardless of the dashes. ;)
Remove the prepending zero from "05"

Converting JSON string into date [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a Unix timestamp to time in Javascript
I'm trying to convert the string that this api returns for date. The format is 1351993013. I tried the following line of JS but the date is completely wrong.
var jsonDate = plugin.versions[0].date;
var pluginDate = new Date(jsonDate);
Which returns:
Fri Jan 16 1970
This is the first time I've tried to format a JSON date so it's a bit confusing. Can anyone help?
That would be seconds, and javascript uses milliseconds which would be
1351993013​000
which would give you Sunday Nov 04 2012.
in other words:
var jsonDate = parseInt(plugin.versions[0].date, 10) * 1000;

How to format the output of the javascript function date() ? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to format javascript date
I have the following bit of code
$('#ins_date').attr('value', Date());
What i get is this : Wed Mar 21 2012 17:52:32 GMT+0100 (Romance Standard Time)
How could i format this string to get somethingh like this : 2012-03-01
date.js is the best I've found. It supports many different formats.
Best option for now is using moment.js library. Formatting for native Javascript Date is not well supported.
var now = moment();
now.format("YY-MM-DD");
$('#ins_date').attr('value', now);

Categories

Resources