How to convert a GMT timestamp to Unix timestamp (javascript) - javascript

I'm parsing an rss feed and each entry has a timestamp such as this: 2009-09-21T21:24:43+04:00
Is there any way to convert this date format to a unix timestamp with javascript?
I just want to be able to record the latest rss entry date, but I need to standardize the time to GMT time or Unix timestamp to do this.

The format appears to be ISO-8601. Assuming that is true, this blog provides a javascript solution to this.

As I met the same problem, the solution involving Google Closure Library would be:
var pattern = "yyyy-MM-ddTHH:mm:ssZZZZZ";
parser = goog.i18n.DateTimeParse(pattern);
var d = new Date();
parser.parse("2009-09-21T21:24:43+04:00", d);
unixTime = d.getTime();

Related

Converting Timestamp String with Timezone into UTC in Javascript Without External Packages

I have a timestamp string and a timezone, and I need to convert that into a Date object in UTC without using moment-timezone.
Essentially, I wanna be able to do this without external dependencies:
var date = moment.tz("2021-03-03 14:40:40", "Asia/Dhaka")
Is this possible? I'd rather not have to download a rather hefty package just for this.
Check it out
let dateString = new Date("2021-03-03 14:40:40").toDateString('en-US', {timeZone: 'Asia/Dhaka'})
console.log(dateString);

Issues parsing a string-date React/JS

For example, I have this string "2020-09-09T21:00:14.114-04:00"
I grab this from my database and in its current form, it is a string. my goal is to have it display
4 PM instead of the long string of jibberish
is it possible to accomplish this?
I was thinking of possibly creating a new date object like
let test = new Date('2020-09-09T21:00:14.114-04:00').
but I'm stuck at the parsing and formatting part. it would be better to have this be done while the current state is a string but I don't think that this would be possible
edit: i would like the desired output to be the hour:minute and then am/pm
ex 10:15pm
You can do that by parsing the date from your database using Date.parse().
Then you can get the time or whatever you need using date.toLocalTimeString() in your case.
let dateUnix = Date.parse('2020-09-09T21:00:14.114-04:00');
const time = new Date(dateUnix).toLocaleTimeString();
console.log(time); // --> "4:00:14 AM"
The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).
Here's some useful resources MDN Date.parse()
MDN Date.toLocalTimeString()
You can do as following way.new Date() is used to get the current date and time.
var today = new Date();
var time = today.getHours();
if(time>12){
var new_time= time % 12;
}
else{
var new_time= time;
}

Converting Moment utc time into unix time

Hello guys I was wondering how do I convert a date into Unix time stamp using the library moment.js so I can compare the oldDate with another date.
This is what I tried:
var oldDate = (moment.unix(1490632174)).format();
// here I got the Date in string format
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
// now I want to convert it again into unix timestamp and I don't know how to do it.
console.log(oldDate, newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Documentation is a wonderful thing. Unix Timestamp (seconds)
moment().unix();
moment#unix outputs a Unix timestamp (the number of seconds since the Unix Epoch).
moment(1318874398806).unix(); // 1318874398
This value is floored to the nearest second, and does not include a milliseconds component.
var oldDate = moment.unix(1490632174).unix();
// here I got the Date in string format
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
// now I want to convert it again into unix timestamp and I don't know how to do it.
console.log(oldDate, newDate.unix());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
You can use the < and > operators:
var oldDate = moment.unix(1490632174);
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
console.log(oldDate<newDate, oldDate>newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Another way you could approach this is to leverage the native Date.parse("2017-03-27T18:29:59+02:00 GMT") method in Javascript. This method parses a date string and returns Unix Time in ms.
For more info, checkout Date.parse() documentation and this Unix Time Converter that makes use of both moment and the native JS method.

Convert time zone of XML parsed time and date with moment.js

I'm parsing a XML file with Javascript and I would like to convert a date to my local time zone using moment.js but I'm stuck. The basic parsing consists of getting the date:
document.write(x[i].getElementsByTagName("Date")[0].childNodes[0].nodeValue);
Which generates something like 31/12/2016 23:00. With moment.js it's possible to format the date like this:
var utcDate = moment.utc('31/12/2016 23:00', 'DD/MM/YYYY HH:mm');
var localDate = utcDate.local();
document.write(localDate);
Which writes 01/01/2017 01:00 in my current time zone. But I can't figure out how to use the method above with the parsing. Tried modifying the variable but only getting "Invalid date" as a result.
var utcDate = moment.utc('x[i].getElementsByTagName("Date")[0].childNodes[0].nodeValue', 'DD/MM/YYYY HH:mm');
var localDate = utcDate.local();
document.write (localDate);
Does anyone have any tips? Might be other solutions than using moment.js but it seemed like the best and most flexible option.
You've put your XML traversal inside a string. That traversal won't happen if it's not actual javascript. Additionally, moment.js will try to parse that literal string as a date, NOT the value from that traversal.
'x[i].getElementsByTagName("Date")[0].childNodes[0].nodeValue'
You need to unquote your traversal to get its value, then feed that to moment.js.
var utcDate = moment.utc(x[i].getElementsByTagName("Date")[0].childNodes[0].nodeValue, 'DD/MM/YYYY HH:mm');

javascript date from php unix timestamp

In database i have a row with date & time, say 2014-04-16 00:00:00 then I convert that datetime to unix timestamp using
strtotime('2014-04-16 00:00:00') * 1000; // result 1397577600000
In javascript i am trying to get the hour using the following code
var d = new Date(1397577600000); // 1397577600000 from php unix timestamp in previous code
d.getHours(); // return 23
d.getMinutes(); // return 0
why getHours() return 23 instead of 0? is there any difference between js timestamp and php timestamp?
Date objects in javascript will always return values based on the browser's current timezone. So if d.getHours() is returning 23 for you, that would suggest your local browser timezone is one hour earlier than UTC (-01:00).
It you want the hours for a Date object based on UTC timezone, you can use:
d.getUTCHours()
Follow Up:
Just to throw out some free advice, you could use the following code to deal with your date values from one context to another:
PHP:
// Fetched from the db somehow
$datetime_db = '2014-04-16 00:00:00';
// Convert to PHP DateTime object:
$datetime_obj = new DateTime($datetime_db, new DateTimeZone('UTC'));
// Format DateTime object to javascript-friendly ISO-8601 format:
$datetime_iso = $datetime_obj->format(DateTime::W3C);
Javascript:
var d = new Date('2014-04-16T00:00:00+00:00'); // 2014-04-16T00:00:00+00:00 from PHP in previous code
d.getUTCHours(); // returns 0
This keeps the datetime variables in a language-specific object format when being handled in that language, and serializes the value into a string format that all current browsers/languages accept (the international standard ISO-8601).
I am getting 21 here because in Javascript local timezone of the user will be considered to fetch time and date.
Ok, based on what Arun P Johnny said.
I change the strtotime parameter to match my timezone, in this case i changed it to
strtotime('2014-04-16 00:00:00 GMT+7') * 1000;
hope this help anybody that have the same problem as I.

Categories