Getting NaN only on certain dates when converting with getTime() [duplicate] - javascript

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
I'm working on a react app with firebase firestore for the db, the app stores a date field obtained from a datepicker in dd/mm/yyyy format as a string. I need to sort by dates and I'm converting dates to correctly sort by numbers with:
new Date(myDate).getTime()
But I'm getting NaN only on certain dates, for example:
01/12/2017 // 1484190000000
02/11/2017 // 1486782000000
22/08/2017 // NaN
Any idea???

var dateArr = '22/08/2017'.split('/');
new Date(dateArr[2], --dateArr[1], dateArr[0]).getTime();
Try this. 22/08/2017 - wrong format, need 08/22/2017

Related

JavaScrip toLocaleDateString() prints wrong date [duplicate]

This question already has answers here:
Convert UTC Epoch to local date
(16 answers)
Closed 4 months ago.
i want to print this date: 21.10.2022
with following timestamp: 1666344563
my code:
new Date(parseInt(element.createdAt)).toLocaleDateString())
FYI element.createdAt = '1666344563'
it prints in chrome: 20.01.1970
can someone explain whats happening here?
JavaScript uses miliseconds timestamps. Your value is made from seconds. To make it work just multiply this value by 1000 like so:
new Date(parseInt(element.createdAt) * 1000).toLocaleDateString())
#EDIT:
Thanks #NineBerry for noticing. Multiply after parsing.

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

Javascript New Date returning me Invalid Date when I passed the date as "21-04-2017 20:34:30 UTC +05:30" [duplicate]

This question already has answers here:
Parse DateTime string in JavaScript
(9 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
I am using angular JS, my Server returning a date like below.
When I tried to convert it to Date format it is saying "Invalid Date".
Please any suggestions?
Below is the sample of server date.
function runSettlement() {
return Restangular.one('/api/GetdateofLastrun').get().then(function
(data) {
var serverDate = new Date(data);
Console.log(data); //Invalid Date
}
Here the API call returns below data from server.
21-04-2017 20:34:30 UTC +05:30
JavaScript Date Objects don't support that format.
I would recommend https://momentjs.com/
"Parse, validate, manipulate, and display dates and times in JavaScript."
Angular directives for Moment.js can be found at: https://github.com/urish/angular-moment

Instance Date with dd/mm/YYYY format [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 7 years ago.
I'm trying to instance a Date in this way
var date = new Date('20/08/1990');
but its returning Invalid Date.
How can a I do it?
When passing a string to Date constructor you should use English notation for dates, i.e YYYY/MM/DD.
That means you should be passing new Date('1990/08/20');
Documentation about Date class here.

Javascript backwards date, reverse? [duplicate]

This question already has answers here:
What is the best way to convert date from JavaScript string in format YYYYMMDD to JavaScript date?
(2 answers)
Closed 9 years ago.
does anybody know How I could reverse the date that I have in javascript?
I have a created date of files from google drive which I pulled using the google drive api, however the date is reverse.
2014-02-25
I would like, 25-02-2014 (dd/mm/yyyy)
var date = resp.createdDate;
date = date.substring(0, date.length - 14)
This is my code, i trimmed the end off, because it came with time etc.
my output is currently : 2014-02-25
Im unsure how I could do this and im sure its simple!
> "2014-02-25".split('-').reverse().join('-')
"25-02-2014"

Categories

Resources