0

i would to get timestamp by UTC Date in javascript. I searched and read other questions but i didn't find a solution.

I have this UTC Date 2017-07-16 12:00:07.8 UTC but if i use .getTime() it returns timestampt (milliseconds) in UTC removing another two hours.

I'm in Italy and here Date is UTC+2, maybe .getTime() read data with Italy timezone and when returns timestamp removing that 2 hours? Is it possible?

var data= new Date(2017,06,16,12,0,7);
var mill= data.getTime();

alert(mill);

OUTPUT

1500199207

I hope you can help me and do not consider my question as duplicate. Thanks...

Borja
  • 3,359
  • 7
  • 33
  • 66

3 Answers3

2

You can use moment to do it:

http://momentjs.com/timezone/docs/

or

Date object is working using UTC. You can add 2 hours in your case using new Date().getTimezoneOffset(); (witch is negative (-120 in minutes))

NikNik
  • 2,191
  • 2
  • 15
  • 34
1

You have to first get your timezoneoffset and add that timezone diff. into data

var data = new Date(2017, 06, 16, 12, 0, 7);
var timestamp = -data.getTimezoneOffset() * 60;
var mill = data.getTime();
mill = mill+timestamp;

Hope this will helpful for you.

Nitin Vaghani
  • 297
  • 4
  • 14
1

Use Date.UTC() instead of new Date():

var mill= Date.UTC(2017,06,16,12,0,7);

alert(mill);

new Date() assumes the values provided are for the browser's current timezone

Lennholm
  • 7,205
  • 1
  • 21
  • 30