As you might know, you can display the time in JavaScript by doing Date() But can you display the Linux Epoch time in JavaScript?
I would like to know because I have a time converter and It would be nice to show the current Epoch time.
You have to use the .getTime() method of the Date object.
var d = new Date();
alert(d.getTime());
Epoch is also called timestamp.
Simple and straightforward.
var milliseconds = new Date().getTime();
Older browsers:
(new Date).getTime()
New way:
Date.now()
Returns in milliseconds, so epoch / 1000 | 0 to get seconds.
Related
Hi this is incredibly simple i cant believe im asking this.
I have tried the following:
Moment.now()
Date.now()
new Date().valueOf()
And various other tricks found in other stack overflow questions
They all give me the time in microseconds.
e.g
1543409290654
which is 09/10/50878 # 10:57am (UTC)
Im aware i could divide by 1000 but surely there is an api in javascript to get the unix timestamp in milliseconds.
Ive have seen this in chrome and react native
EDIT:
So i realise my stupidity i was putting the unix timestamp into a website that renders it as an ISO date but it was expecting seconds which is why i thought my dates were coming in as milliseconds
Date.now() returns the number of milliseconds since midnight January 1, 1970
There is no native javascript to format time into unix-timestamp.
I found this useful
var today = Math.round((new Date()).getTime() / 1000);
console.log(today);
I'm trying to get the same value as 1502755200000 using momentjs
console.log(Date.UTC(2017, (8-1), 15));//1502755200000
var newDate = moment();
newDate.set('year', 2017);
newDate.set('month', 7); // April
newDate.set('date', 15);
console.log(newDate.format('X'));//1502818350
However when I try to get the miliseconds I get 1502818350 Any idea how to get the exact same timestamp as above?
Here is the fiddle https://jsfiddle.net/cdvzoezb/1/
Firstly, .format('X') gives you the unix timestamp in seconds, not milliseconds.
To get milliseconds, you must use .format('x') (lowercase x).
Secondly, when you use moment(), it gives you a moment date object at your current local time, not UTC time. So when you modify it with .set('date', 15) etc, you're setting it to 15 April 2017 in your local time. This is why you're getting the vastly different value.
To get a moment date object for current UTC time, use moment.utc().
Thirdly, the Date object you created will be at time 00:00:00.000, while the moment object will be for current time. So when you set the year/month/date, time still remains at what it was when you created the object. You need to set the moment object's time to 00:00:00.000.
This can be done with the .startOf('day') function.
In conclusion:
console.log(Date.UTC(2017, (8-1), 15)); //1502755200000
var newDate = moment.utc();
newDate.set('year', 2017);
newDate.set('month', 7);
newDate.set('date', 15);
newDate.startOf('day');
console.log(newDate.format('x')); //1502755200000
Or, much shorter:
var newDate = moment.utc('2017-07-15 00:00:00.000');
Well you can simply create a moment object out of a Date instance and then using the utc() to convert the timestamp to UTC. After that, we can use moments method format() to get milliseconds using the x display option like so:
console.log("==============");
console.log(Date.UTC(2017, (8-1), 15));
var base = Date.UTC(2017, (8-1), 15)
var newDate = moment(base);
console.log('a', newDate.utc().format('x')); //1502755200000
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?
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
You already have the value you want.
var numberOfMillisecondsSinceEpoch = Date.now();
You're attempting to call methods on a Date object, such as you'd get for the current date by calling new Date(). That's not necessary or appropriate if you're using Date.now(), which returns a number instead.
For platforms that don't provide Date.now(), you can convert the current Date object to a number to get the same value.
var numberOfMillisecondsSinceEpoch = Number(new Date());
Number(new Date()) === Date.now() // if your system is quick enough
So, I need to convert this kind of Date in ISO format:
"05-Mar-13 17:00:00.000000"
But when I do something like this:
var Time = (new Date("05-Mar-13 17:00:00.000000")).toISOString().replace('Z', Milliseconds);
I've got in variable Time another hour:
"2013-03-05T16:00:00.000000"
So It changes on another hour.
What should I do to avoid this changing of hours?
The ISO format is supposed to convert the time to UTC.
Your browser supposes that the time you are passing the Date constructor is based on your local time, supposedly one hour behind UTC.
To counter this, you can use
new Date().getTimezoneOffset();
which will return the time offset in minutes. In your case it will return -60.
A complete example:
function getTime() {
var date = new Date("05-Mar-13 17:00:00.000000")
date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
return date.toISOString();
}
Note that this can set time minutes to a negative value, but the Date object is smart enough to convert that to a new time with positive minutes again by changing the hour.
Hi I'm passing a unixtimestamp to a javascript IF statement, can anyone tell me how to generate a unixtimestamp one minute in the future with javascript.
Anyhelp would be helpful.
Thanks
The JavaScript Date object has a getTime() method that returns milliseconds since 1970. To make this look like a UNIX timestamp, you need to divide by 1000 and round (with Math.floor()). Adding 60 get's your one minute ahead.
var d = new Date();
var unixtimeAdd60 = Math.floor(d.getTime()/1000)+60;
UNIX time is just the number of seconds since 1970-01-01Z. So just add 60 you'll get a timestamp one minute later.
JavaScript Date object's getTime returns the number of milliseconds since midnight Jan 1, 1970.
Try this.
var oneMinLater = new Date().getTime() + 60 * 1000;
var d = new Date();
d.setTime(oneMinLater);
Another way to get the unix timestamp (this is time in seconds from 1/1/1970) in a simple way its:
var myDate = new Date();
console.log(+myDate + 60); // you just sum the seconds that you want
// +myDateObject give you the unix from that date