This question already has answers here:
How do I get a timestamp in JavaScript?
(43 answers)
Closed 9 years ago.
In python I can use time.time() to get this:
time = time.time()
time = 1362914221
How do I get this in java-script?
time = ????
You can use
time = (new Date()).getTime() / 1000;
See getTime() on the MDN.
Just like this:
var time = +new Date;
Related
This question already has answers here:
Using PHP to Match a Javascript Timestamp
(3 answers)
Closed 3 years ago.
In JavaScript I am using (new Date("1985-05-01")).getTime(); to get the timestamp. The return value is 483753600000.
In PHP I would like to return the year of that timestamp.
date('Y', 483753600000); returns 17299 but 1985 is expected.
What am I doing wrong?
JavaScript's getTime() will return timestamp in miliseconds. PHP's date() function needs timestamp in seconds. So just divide it by 1000:
date('Y', 483753600000 / 1000);
This question already has answers here:
Javascript Convert Date Time string to Epoch
(12 answers)
Closed 7 years ago.
I'm using the Hacker News API made at Algolia here:
https://hn.algolia.com/api
I'm a bit confused as it says to search for posts since a certain time it says to run the following query:
Comments since timestamp X (in second)
http://hn.algolia.com/api/v1/search_by_date?tags=comment&numericFilters=created_at_i>X
It says to replace X with a timestamp in seconds, but how exactly would you do this? Let's say the last post I have is at 2015-08-25T15:35:58.000Z. How exactly would I run this query to search for posts since that date? I don't know how to convert this date to seconds...
getTime() will get the date in milliseconds, so divide by 1000:
var date = new Date("2015-08-25T15:35:58.000Z");
var seconds = date.getTime() / 1000; //1440516958
This question already has answers here:
How do I get a timestamp in JavaScript?
(43 answers)
Closed 7 years ago.
Is it possible for me to get the UTC time of current hour in Javascript?
I've tried the following:
var mins = new Date().getMinutes();
var hours = new Date().getHours();
var hourStamp = new Date().setHours(hours, 0,0,0);
var dates = new Date(hourStamp);
Here dates gives me the Unix Timestamp of the current hour. Is there a better/faster way to do this?
EDIT:
For example: I want the Timestamp for the current hour. So if it's 00:16 am on 12/04/2015, I want the timestamp of 12/04/2015 00:00. And, my method works. My question pertains to if there's a better method of doing the same.
var UTCHour = new Date().getUTCHours();
Like this?
The one works well:
var UTCTime= (new Date()).toUTCString();
This question already has answers here:
Calculating milliseconds from epoch
(5 answers)
Closed 8 years ago.
I have a string the represents a time "2014-07-03T11:47:00"
I would like to covert this time to milliseconds and I think it should look something like the following.
var myTime = Date.parse("2014-07-03T11:47:00");
var myTimeMs = myTime.getTime();
Any suggestions about how to get the value in milliseconds on any browser?
Date.parse returns the time in milliseconds
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get current date in JavaScript
How can i Get the time out of my browser whit using javascript?
This do I need to make a comparison in my script.
currentTime = new Date();
time = currentTime.getTime();
hours = currentTime.getHours();
etc
You can use the Date object to get the current time:
var currentTime = new Date();