So if i see '1328571956' as the value for the current visit, how do I decode that to a readable value?
thx
don't forget to multiply seconds by 1000 for unix to javascript timestamps:
Date.fromUnix=function(n){
return new Date(n*1000);
}
alert(Date.fromUnix(1328571956).toUTCString());
It's a UNIX timestamp. For decoding it into a usable JavaScript date object, there's this: Convert a Unix timestamp to time in JavaScript
As #wooble above stated... "That's seconds after the start of the UNIX epoch (1 Jan 1970, 0:00 UTC)"
To use it though, do this...
var date = new Date(1328571956).toString();
Related
I want to create JavaScript countdown, but I have to use atomic clock (non local PC time).
I find the http://worldclockapi.com/api/json/est/now, which gets you a JSON.
There is a property currentFileTime (timestamp), but not UNIX timestamp.
How to convert this timestamp to UNIX timestamp, get the Date Object and check Date, Hours, Minutes and Seconds?
Thanks
Just pick a timezone and call that:
With a call to http://worldtimeapi.org/api/timezone/Europe/London.json
You will get a json object with a unix time like here:
let result = {"week_number":29,"utc_offset":"+01:00","utc_datetime":"2019-07-15T11:44:07.720355+00:00","unixtime":1563191047,"timezone":"Europe/London","raw_offset":0,"dst_until":"2019-10-27T01:00:00+00:00","dst_offset":3600,"dst_from":"2019-03-31T01:00:00+00:00","dst":true,"day_of_year":196,"day_of_week":1,"datetime":"2019-07-15T12:44:07.720355+01:00","client_ip":"194.153.217.248","abbreviation":"BST"}
// result["unixtime"] => 1563191047
I am getting timestamps for estimated bus arrival times from an API as a timestamp / epoch: 1536589019000. If I go to a website like this I get the appropriate format:
Monday, September 10, 2018 7:16:59 AM
But if I attempt to convert the date in javascript, for example, with momentjs, I get some date far into the future: 50662-08-08 08:03
moment.unix(estimatedArrivalTime).format("YYYY-MM-DD HH:mm")
How do I convert a unix timestamp properly?
divide the time by thousand , moment.unix() expects time to be in seconds
moment.unix(1536589019).format("YYYY-MM-DD HH:mm")
You're getting a timestamp in millesconds instead of seconds. Just passing it like so should work: moment(1536589019000).format("YYYY-MM-DD HH:mm")
I'm returning a date from json and parsing it to a date format like so;
var date = new Date(parseInt(date.substr(6)));
The problem is the date field in the database is set to 23:59:59.000 for the time.
The above code returns the day after using date.getDate().
I'm assuming this is due to the time.
How can i return the accurate date with the time being set to 23:59:59.000
Cheers
Edit
Incase anyone comes across this i fixed it by using;
var utc = new Date(date.getTime() + date.getTimezoneOffset() * 60000);
Assuming that the database is storing the UNIX Epoch (in secs), try multiplying the parseInt with * 1000. This will also set the miliseconds to 0.
let timeFromDB = parseInt(date.substr(6)) * 1000
let date = new Date(timeFromDB);
Date API
Integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC, with leap seconds ignored (Unix Epoch; but consider that most Unix timestamp functions count in seconds).
The question was somewhat unclear, but hope this helps.
I am using moment.js to convert a bunch of timestamps in it's specific timezone to a unix timestamp like this:
var timestamp = "2015-12-29T09:35:00.000-08:00";
console.log(moment("2015-12-29T09:35:00.000-08:00").unix();
console.log(moment("2015-12-29T09:35:00.000-08:00").tz("America/Los_Angeles").unix();
The console log of both the above statements is for some reason, the same - 1451361900. This unix timestamp which it is logging is in my local timezone and not the one I asked for: "America/Los_Angeles". What am I missing?
A unix timestamp, or Posix, should always be in the UTC (Coordinated Universal Time) format.
Moment is just doing something like
function unix () {
return Math.floor(+this / 1000);
}
Where it converts the date object to an integer and then converts from milliseconds to seconds.
The starting point is a regular javascript Date object, and the ECMA standard says
Date objects are based on a time value that is the number of
milliseconds since 1 January, 1970 UTC.
so date objects are always UTC when converted to the number of milliseconds since 1. January 1970 (epoch), i.e. you can't set another timezone on a Unix timestamp, both your dates are the same.
The proper way is to use moment-Timezone is this.
console.log(moment("2015-12-29T09:35:00").unix());
console.log(moment.tz("2015-12-29T09:35" , "America/Los_Angeles").unix());
In above your are providing time zone as a string too which is this last part ".000-08:00" and then you are providing another zone, which is incorrect.
As you are trying to find out the unix timestamp for the date "2015-12-29T09:35:00.000-08:00". In this date format timezone value is already present which is "-08:00", hence you get the same unix timestamp.
For getting the unix timestamp desired solution, remove the timezone value and use moment-timezone as :
console.log(moment.tz("2013-12-01", "America/Los_Angeles").unix());
For more details check moment-timezone
Heya heres a (hopefully) easy one,
how can I convert this DATETIME value:
2014-01-16 12:00:00
to an unix timestamp via JavaScript or JQuery?
In PHP It is easy but in JavaScript it seems hard. I found out that Date.parse() or new Date() provide me that but this only helps with timestamps containing this other format with day of the week.
Is there a way to convert this 'numeric' variant aswell?
Thanks in advance~
In order to convert this string to a Javascript date format, you need to replace the space character between date and time with the character T in order to create a valid ISO8601 format. Then you can use .getTime() for the unix timestamp of this date.
var timestamp = new Date('2014-01-16 12:00:00'.replace(' ', 'T')).getTime();
You could do this, but note 2014-01-16 12:00:00 is the local time of the machine.
var ts = new Date('2014-01-16 12:00:00').getTime() / 1000;
Update:
As #devnull69 said, Firefox only accepts the ISO format, you have to replace the space to T.
Date.prototype.getTime returns a unix timestamp in milliseconds. If you want it in seconds, divide it by 1000.
function mysql_to_unix(date) {
return Math.floor(new Date(date).getTime() / 1000);
}
mysql_to_unix("2014-01-16 12:00:00"); // returns 1389870000
<script>
function test(date){
n=date;
var d=new Date(n).getTime() / 1000
console.log(d);
}
</script>
<body onLoad="test('2014-01-16 12:00:00')">
</body>