0

I want to show the upload date of a photo in my page, but I just can display the date according to the server time zone.

Let's suppouse I uploaded the photo at home the 12th at 19:30 GMT-5, so the server registered the 13th at 00:30.

What I want is to retrieve the date known by the user (12th 19:30).

What I've done is to get the timezone of the client and sustract that amount of minutes to the object Date, like this:

upload_date = response.upl_date;//This is the date I get from server (12:30)

var d = new Date();//Here I get the current date of client...
var timezone = d.getTimezoneOffset();//...to get the timezone offset in minutes (300)

var dd = new Date(upload_date);//Here I turn my server date into an object

dd.setMinutes(dd.getMinutes() - timezone);//Substract the minutes to the hour of the object

console.log(dd);//And voilá, I get 19:30, but the day keeps being 13

Despite the minutes affect the hours of the object Date, it doesn't affect the day, so it keeps being 13th instead of 12th.

Of course it works perfectly when, for example, it's 19:00 on server side and 14:00 on client side, but what can I do to change the days to fit with the timezone change? Should I try another way? Or some trick with if(){} ?

  • when the user uploads the image, save the GTM to the server. when retrieving retrieve and convert to client time zone accordingly – Biswajit Nath Apr 13 '19 at 04:05

1 Answers1

0

I hope this will be helpful to you:

var options = {
  timeZone: "America/New_York", //you can use any timezone here
  year: 'numeric',
  month: 'numeric',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric'
};

var formatter = new Intl.DateTimeFormat([], options);

var UTCTime = "2017-09-03T02:00:00Z";
var localTime = formatter.format(new Date(UTCTime));
console.log("UTC to NY:", localTime);

var currentTime = formatter.format(new Date());
console.log("Current time in NY:", currentTime);
Daniel Compton
  • 13,878
  • 4
  • 40
  • 60
gopinath
  • 61
  • 1
  • 11