JavaScrip toLocaleDateString() prints wrong date [duplicate] - javascript

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.

Related

How can I check in javascript if it's day time or night time on the client? [duplicate]

This question already has answers here:
Check if current time is between two given times in JavaScript
(5 answers)
Closed 3 years ago.
Using date-nfs, how can I check if new Date() is between 8am and 8pm?
You don't need to use external libraries, you can simply get the current hour by new Date().getHours(), and check if it's more then 20 or less then 8.
getHours() on the date object will be an integer between 0 and 23. So you could check if it's between 8 and 20

Date conversion is returning an old date in javascript [duplicate]

This question already has answers here:
Why do I need to multiply unix timestamps by 1000 in JavaScript?
(3 answers)
Closed 4 years ago.
I have an API which returns date values. Sample value is like 1521441000. When I formatted the date using javascript, I am getting Jan 18 1970 08:07 PM. Also I tried using Javascript Date function. Then also the same reply. Please see the console screen shot. As per the expectation I want this months(March - April 2018) date. Which I am fitering using API call. This is a third party API.I want to confirm is this the problem with API.
Why this is happening. Is this the problem with the date which is returned from the API? Or Is there any problem in my comparison?
1521441000 is time in seconds, you need to multiply that by 1000 to get miliseconds before passing to moment/date constructor:
console.log(new Date(1521441000));
console.log(new Date(1521441000 * 1000));

How to display properly time on client? [duplicate]

This question already has answers here:
Is there a simple way to convert a decimal time (e.g. 1.074 minutes) into mm:ss format using moment.js?
(4 answers)
Closed 5 years ago.
I send TimeSpan object called time from back-end to front-end.
Here how the type looks in back-end:
I have this type on client side:
I need to display time variable only hours:minutes:seconds.
How can I convert the type above(i.e. time) to format above?
var x = new Date(1561000); //replace with TotalMilliSeconds in your case
var time = x.toUTCString().split(' ')[4]; //slit by space character
console.log(time);

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

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

Calculate time difference beetween two dates with javascript [duplicate]

This question already has answers here:
Get difference between 2 dates in JavaScript? [duplicate]
(5 answers)
Closed 8 years ago.
I have two dates in format: Y-m-d H:i:s
How I can calculate time beetweeb two dates:
data.endTime - data.startTime
The best way is like #Paul S. say, convert them into Date, and then yo can make something like this:
var resultInMs = date_end.getTime() - date_start.getTime();
Hope it works for you.
PD: Here is something that can help you http://www.w3schools.com/jsref/jsref_obj_date.asp
PD2: Like #James Thorpe says Mozilla Docs is also a very good source of information (Even better): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Categories

Resources