Diogo Martinho
Diogo Martinho

Reputation: 137

Converting date to unix with milliseconds precision in Javascript

I am currently trying to convert a few dates in Javascript however these dates are defined with milliseconds and that data cannot be lost when converted. I have tried doing the following

var dateString = '2009-07-15 11:00:00.675';
dateString = dateString.split(' ').join('T');
var date = new Date(dateString);
date = date.getTime() / 1000;

But date returns

date= 1247655600.675

I have read a few topics in stack overflow but the only "solution" I saw was the following:

parseInt((new Date('2012.08.10').getTime() / 1000).toFixed(0))

But this does not consider milliseconds either. What should I do to correctly convert a date to unix timestamp with milliseconds precision?

Thank you.

Upvotes: 1

Views: 1285

Answers (1)

Fanyo SILIADIN
Fanyo SILIADIN

Reputation: 792

try this, no need to do /1000

var dateString = '2009-07-15 11:00:00.675';
dateString = dateString.split(' ').join('T');
var date = new Date(dateString);
date = date.getTime();

Upvotes: 2

Related Questions