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>
Related
I see a lot in the Moment.js documentation about getting a Moment from a Unix timestamp. However, I am trying to convert a Moment to a Unix timestamp, and I am not sure how to do that. This is how my moment looks:
const myMomentObject = moment(str_time, 'YYYY-MM-DD');
And I need to convert it to a Unix timestamp.
Unix timestamp can be obtaineded with or without the moment library.
//Moment
const myMomentObject = moment('2021-10-16', 'YYYY-MM-DD');
console.log(myMomentObject.unix());
//Vanilla
const d = new Date('2021.10.16');
console.log(d.getTime() / 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Call unix() on moment object. Reference.
moment('2020-12-12').unix()
To create a Unix timestamp (seconds since the Unix Epoch) from a moment, use the following,
console.log(moment('2021-10-16').unix());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Since your date format follows the ISO_8601 format, i.e. YYYY-MM-DD you do not need to provide the input date format to the moment constructor.
console.log(new Date('2016-05-24').toISOString()); // '2016-05-24T00:00:00.000Z'
console.log(new Date('05/26/2016').toISOString()); // '2016-05-23T23:00:00.000Z' // why?
I am sending data to the server to parse and want to ensure that server will encode my date correctly.
What is the simplest way to convert date to string as '2016-05-24T00:00:00.000Z' in both cases?
Thanks
console.log(new Date('2016-05-24 GMT').toISOString()); // '2016-05-24T00:00:00.000Z'
console.log(new Date('05/24/2016 GMT').toISOString()); // '2016-05-24T00:00:00.000Z'
Append the timezone to the date before creating a new date object so that the string parsing code in the Date constructor doesn't get confused. Always disambiguate if possible.
Your code was using different timezones for each parse because of the way the dates were formatted. One was using +0 timezone, other was using -1 timezone hence the date being pulled back an hour when the ISO string was created.
One is parsing in UTC time, one is parsing in local time.
new Date('2016-05-24').toISOString() // '2016-05-24T00:00:00.000Z'
new Date('05/24/2016').toISOString() // '2016-05-24T07:00:00.000Z'
Playing around, here's one solution:
new Date(new Date('05/24/2016') - (new Date()).getTimezoneOffset() * 60000).toISOString() // '2016-05-24T00:00:00.000Z'
The strategy:
Create the new offset date
Subtract the offset
Create a new date from that result
Reference links:
javascript toISOString() ignores timezone offset
Why does Date.parse give incorrect results?
On further consideration, I'd recommend parsing the date string into something that is "universal" before passing it to the date constructor. Something like:
var tmp = ('05/24/2016').split('//');
var universal = [tmp[2], tmp[0], tmp[1]].join('-'); // 2016-05-24
...
Also, Moment.js does this sort of thing very neatly.
Use the getDate(), getMonth() and getFullYear() methods to strip out what you need.
I'm returning a timestamp from SQL server as an integer (seconds after 1/1/1970). The problem I'm having is that dojo (or javascript) is assuming the timestamp is GMT and then converting it to my local time zone. Is there a way to set the time zone when I create the new date, or tell dojo not to make the conversion? This is my code
function formatDate(value) {
// SQL Server returns seconds -- multiply by 1000 to get milliseconds
var date = new Date(parseInt(value) * 1000);
return ddl.format(date, {//ddl is dojo/date/local
selector: "date",
datePattern: 'MM/dd/yyyy h:m a'
});
}
Because of standard and daylight times I really dont want to add time to my timestamp
Thanks
I found the answer here:
How do you create a JavaScript Date object with a set timezone without using a string representation
var d = new Date(xiYear, xiMonth, xiDate);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
I have a date like this:
2014-04-23T19:45:39 which is a UTC format.
I want to convert it to AST format or the localize time zone of the user. How to do it?
I suggest you use moment.js library and just add or subtract number of hours that AST time have comparing to UTC.
new_date = date.add('hours',4);
or
new_date = date.subtract('hours',4);
using timezone-js. you can easily convert the time from one timeZone to another. timezone.js
var date_object;
function localize(t){
date_object=new Date(t+" UTC");
document.write(date_object.toString());
}
localize("4/24/2014 4:52:48 PM")
document.write(date_object.toString().replace(/GMT.*/g,""));
Demo
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();