如何将时间从IST转换为UTC

时间:2018-03-28 04:30:34

标签: javascript timezone momentjs

我想使用moment.js在IST(印度标准时间)下显示以下时间到UTC,我遇到的逻辑是:

例如: 星期四凌晨4:00 ,等效的UTC时间晚上10:30 星期三

enter image description here

这是我在IST 中的 JSON对象,我正在尝试将其转换为UTC 。有人可以指导我如何实现这一目标。

  {
    "Friday" : {
      "available" : 1,
      "times" : [ {
        "endTime" : "1:30 PM",
        "startTime" : "9:00 AM"
      }, {
        "endTime" : "6:00 PM",
        "startTime" : "2:30 PM"
      } ],
      "weekday" : "Friday"
    },
    "Monday" : {
      "available" : 1,
      "times" : [ {
        "endTime" : "1:30 PM",
        "startTime" : "9:00 AM"
      }, {
        "endTime" : "6:00 PM",
        "startTime" : "2:30 PM"
      } ],
      "weekday" : "Monday"
    },
    "Saturday" : {
      "available" : 1,
      "times" : [ {
        "endTime" : "1:30 PM",
        "startTime" : "9:00 AM"
      }, {
        "endTime" : "6:00 PM",
        "startTime" : "2:30 PM"
      } ],
      "weekday" : "Saturday"
    },
    "Sunday" : {
      "available" : 0,
      "times" : [ {
        "endTime" : "1:30 PM",
        "startTime" : "9:00 AM"
      }, {
        "endTime" : "6:00 PM",
        "startTime" : "2:30 PM"
      } ],
      "weekday" : "Sunday"
    },
    "Thursday" : {
      "available" : 1,
      "times" : [ {
        "endTime" : "1:30 PM",
        "startTime" : "4:00 AM"
      }, {
        "endTime" : "6:00 PM",
        "startTime" : "2:30 PM"
      } ],
      "weekday" : "Thursday"
    },
    "Tuesday" : {
      "available" : 1,
      "times" : [ {
        "endTime" : "1:30 PM",
        "startTime" : "9:00 AM"
      }, {
        "endTime" : "6:00 PM",
        "startTime" : "2:30 PM"
      } ],
      "weekday" : "Tuesday"
    },
    "Wednesday" : {
      "available" : 1,
      "times" : [ {
        "endTime" : "1:30 PM",
        "startTime" : "9:00 AM"
      }, {
        "endTime" : "6:00 PM",
        "startTime" : "2:30 PM"
      } ],
      "weekday" : "Wednesday"
    }
  }

2 个答案:

答案 0 :(得分:0)

您可以使用moment.utc()尝试这样做:



var d = new Date('Mon Jan 01 2018 04:00:00 GMT+0530');
console.log('IST: ',  moment(d).format('hh:mm A'))

var utc = moment.utc(d).format('hh:mm A')
console.log('UTC: ',  utc)

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

我对Moment不熟悉,但可以使用纯JavaScript轻松完成:

const ist2utc = (t) => {
  let d = new Date(t);
  d.setHours(d.getHours() - 5);
  d.setMinutes(d.getMinutes() - 30);
  return d;
};

示例:

let x = ist2utc(new Date())
console.log(x.toString())