Reputation: 51
I have a UTC date string -> 10/30/2014 10:37:54 AM
How do I get the timestamp for this UTC date? As far as I know, following is handling this as my local time
var d = new Date("10/30/2014 10:37:54 AM");
return d.getTime();
Thank you
Upvotes: 1
Views: 972
Reputation: 2175
You can decrease the "TimezoneOffset"
var d = new Date("10/30/2014 10:37:54 AM");
return d.getTime() - (d.getTimezoneOffset() *1000 * 60);
Also u can use the UTC function
var d = new Date("10/30/2014 10:37:54 AM");
return Date.UTC(d.getFullYear(),d.getMonth()+1,d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds());
Upvotes: 1
Reputation: 24915
You can use Date.UTC
to create a UTC format date object.
Reference:
(function() {
var d = new Date();
var d1 = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds()));
console.log(+d);
console.log(+d1);
})()
Upvotes: 2
Reputation: 104
Hope this helps:
Date date= new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
date = sdf.parse(date.toString());
String timestamp = sdf2.format(date);
Upvotes: 0
Reputation: 1960
If the format is fixed, you could easly parse it and use the Date.UTC() API.
Upvotes: 1
Reputation: 1476
Try Date.parse
var d = new Date("10/30/2014 10:37:54 AM");
var timstamp = Date.parse(d) / 1000;
Upvotes: 0
Reputation: 6548
A quick hack would be to append UTC
to the end of your string before parsing:
var d = new Date("10/30/2014 10:37:54 AM UTC");
But I would advise you to use a library like moment.js, it makes parsing dates much easier
Upvotes: 0