Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  159] [ 4]  / answers: 1 / hits: 18713  / 8 Years ago, wed, april 20, 2016, 12:00:00

I am having a lot of trouble doing something that seems obvious. I have a date:


Date.now()

I want it in milliseconds from epoch. I cannot get that to work. I have tried:


Date.now().getTime();
(Date.now()).getTime();
Date.now().getMilliseconds();
(Date.now()).getMilliseconds();

var date = Date.now();
var ms = date.getTime();
var ms = date.getMilliseconds();

All of these fail because apparently getTime() and getMilliseconds() (which I don't think is the correct approach anyways) are apparently not functions.


What am I doing wrong here?


More From » javascript

 Answers
9

Date.now() already returns ms from epoch, not a Date object...



Date.now is a method in the Date namespace1, same as Math.random is for Math.

Date (unlike Math) is also a constructor. Used like new Date(), it will return a Date object.



1. A property of Date, which is a function/object


[#62462] Tuesday, April 19, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darleneh

Total Points: 231
Total Questions: 110
Total Answers: 94

Location: Spain
Member since Thu, Dec 23, 2021
2 Years ago
;