Moment: 从 moment.js 对象中删除时区

创建于 2015-12-04  ·  3评论  ·  资料来源: moment/moment

我正在使用datetimepicker.js ,它的date函数返回一个moment.js对象。 它使用本地 UTC 偏移量来实现,而我的原始日期有不同的偏移量。

我原来的日期:

2015-10-01T15:00:00.000Z

我在日期时间选择器( DD-MM HH:mm )上显示的内容:

01-10 15:00

我得到什么:

2015-10-01T15:40:00+01:00

我想要的是:

2015-10-01T15:40:00+00:00

请注意我是如何在最后删除 +01 偏移量的。

我怎样才能将它应用于任何本地 UTC? 也就是说,无需手动删除 01(因为它可以是任何其他本地偏移量,具体取决于用户位置)。

这是我到目前为止得到的日期,但我不知道如何通过保持相同的时间来消除偏移量。

    var momentDate = timePicker.data("DateTimePicker").date();
    console.log(momentDate.format());
    //this prints  2015-10-01T15:40:00+01:00

最有用的评论

const tmp = moment(valueAsDate).utc(true) - 这就是你需要的忽略时区
那么您可以使用例如tmp.toISOString()

所有3条评论

重复: http ://stackoverflow.com/questions/34050389/remove-timezone-from-a-moment-js-object

因为时刻会将原始日期格式化为本地时区日期。 即 2017-08-02 10:00:00+02:00 将被格式化为 2017-08-02 16:00:00+08:00 如果机器系统使用 +08:00 时区。

我写了一个函数来格式化原始日期忽略时区:

/**
 * <strong i="7">@desc</strong> use moment to format a datetime
 * <strong i="8">@param</strong> string|date|timestamp datetime: the datetime to format
   the given datetime can have timezone, and the timezone will be kept in the process of output datetime
   i.e. formate('2017-09-08 11:24:00+08:00', 'D/M/YYYY HH:mm:ss Z') =>  8/9/2017 11:24:00 +08:00, as you see, +08:00 is kept, which is different from moment default behavior
   if timezone has not been given, local timezone will be used.
 * <strong i="9">@param</strong> string formatter: the target format you want to get
 * <strong i="10">@param</strong> string givenFormatter: moment allow developer to define your own formatter, i.e. DD/MM/YYYY HH,mm,ss which is not a statndard time format
 * <strong i="11">@return</strong> string new formatted datetime string
 * <strong i="12">@example</strong>:
   1. format(new Date(), 'YYYY-MM-DD HH:mm:ss')
   2. format(your_date, 'YYYY-MM-DD HH:mm:ss', 'DD/MM/YYYY HH,mm,ss')
 */
export function format(datetime, formatter, givenFormatter) {
  let localTimezoneOffset = timezoneOffset()
  let givenTimezoneOffset = moment.parseZone(datetime).utcOffset()

  if (givenTimezoneOffset !== 0) {
    return moment(datetime, givenFormatter).utcOffset(givenTimezoneOffset).format(formatter)
  }
  else {
    // big problem: we do not know whether timezone has not been given or it is +00:00
    // if its utc time is the same with itself, it means timezone has been given
    let u = utc(datetime, 'YYYY-MM-DD HH:mm:ss')
    let t = moment(datetime).parseZone(datetime).format('YYYY-MM-DD HH:mm:ss')
    if (u === t) {
      return moment(datetime, givenFormatter).utc().format(formatter)
    }
    else {
      return moment(datetime).format(formatter)
    }
  }
}

源码: https ://github.com/tangshuang/moment-format

使用此函数格式化日期忽略时刻默认格式逻辑。 2017-08-02 10:00:00+02:00 将被格式化为 2017/08/02 10:00:00+02:00。

const tmp = moment(valueAsDate).utc(true) - 这就是你需要的忽略时区
那么您可以使用例如tmp.toISOString()

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

Shoroh picture Shoroh  ·  3评论

ninigix picture ninigix  ·  3评论

BCup picture BCup  ·  3评论

RobinvanderVliet picture RobinvanderVliet  ·  3评论

nikocraft picture nikocraft  ·  3评论