0

i want to change Local time to UTC and vice-versa, by taking users date value.(User will choose the dateand Time and according to dates it will change to UTC and vice versa )

Can someone help on this

2 Answers2

0

If you have your date+time in a JavaScript Date object you can call its getUTCDate()method to get a new Date whose time zone is UTC.

If that is not what you have in mind please be more specific about what you mean by “it will change”.

Renardo
  • 499
  • 4
  • 13
0

You can try this:

function calculateTimestamp(date) {
    date = date.split('-');
    var year = date[2];
    var month = date[1];
    var day = date[0];
    var d1 = new Date(Date.UTC(2017, 9, 1, 17, 0, 0, 0)); //It is static time based upon you will count time in ms
    var d2 = new Date(Date.UTC(year, (month - 1), day, 17, 0, 0, 0));
    return parseInt((d2.getTime() - d1.getTime()) / 1000);
}

var startDate = '27-02-2018';
var startTime = calculateTimestamp(startDate);

Now you can convert this timestamp again as below:

var d = new Date(Date.UTC(2017, 9, 1, 17, 0, 0, 0));
var t = parseInt(d.getTime() / 1000);

var d = new Date((startTime + t) * 1000);
HOTAM SINGH
  • 121
  • 2
  • 11