简体   繁体   English

如何将此日期从momentjs转换为纯JavaScript

[英]How do I convert this date from momentjs to plain JavaScript

I have a timestamp that I am trying to roundto the nearest UTC Monday 00:00:00:00:000Z 我有一个时间戳,试图将其舍入到最近的UTC星期一00:00:00:00:000Z

My code in moment looks like this 我的代码此刻看起来像这样

let now = Date.now()
moment.unix(now / 1000).utc().startOf("isoWeek").valueOf()

I am trying to do this in plain JS without moment and I am not getting the same answer 我正在尝试用普通JS进行此操作,而没有得到相同的答案

const nearestMonday = date => {
    const monday     = 1;
    const currentDay = date.getDay();
    const distance   = (monday + 7 - currentDay) % 7;
    const newDate    = new Date(date.getTime());
    newDate.setDate(date.getDate() + distance);
    newDate.setHours(0, 0, 0, 0);
    return newDate;
}

> d = Date.now()
1545989455067
> nearestMonday(new Date(d)).getTime()
1546194600000
> m.unix(Date.now() / 1000).utc().startOf("isoWeek").valueOf()
1545609600000

I am in GMT + 530 zone , what do I change to get the same answer as moment 我在GMT + 530区域中,该如何更改以得到与瞬间相同的答案

I think this may do what you want: 我认为这可能会满足您的要求:

const nearestMonday = date => {
    const day = 1000*60*60*24;
    const week = day*7;
    return new Date(Math.floor(date.getTime()/week)*week-3*day);
}

Ok, so we have a few problems here: 好的,所以这里有一些问题:

First: Timezones 第一:时区

Date works with your local timezone, so when you do newDate.setHours(0, 0, 0, 0); 日期与您的本地时区一起使用,因此当您执行newDate.setHours(0, 0, 0, 0); and stuff like that, it sets the object to that hours in your timezone. 诸如此类的东西,它将对象设置为您所在时区的那个小时。 When you do .getTime() , however, it does return millis from epoch in UTC. 但是,当您执行.getTime()时,它会从UTC的纪元返回毫秒。

The result of this being: if you are in gmt+530 (India, I believe) when you do a .getTime() the millis from epoch will be off by that difference (5h 30m). 结果是:如果您使用的是gmt + 530(我相信是印度),那么当您执行.getTime()时,纪元的毫秒数将相差5h 30m。

To compensate that, you can use getTimezoneOffset() : 为了弥补这一点,您可以使用getTimezoneOffset()

const nearestMonday = date => {
    const monday     = 1;
    const currentDay = date.getDay();
    const distance   = (monday + 7 - currentDay) % 7;
    const newDate    = new Date(date.getTime());
    newDate.setDate(date.getDate() + distance);
    newDate.setHours(0, 0, 0, 0);
    newDate.setTime(newDate.getTime()-1000*60*newDate.getTimezoneOffset());
    return newDate;
}

On the other hand, your code using moment will work properly with timezones, so there's no need to change it. 另一方面,您使用代码的代码将在时区正常工作,因此无需更改它。


Second: What monday? 第二:什么星期一?

Your function nearestMonday calculates the next Monday. 您的函数nearestMonday计算下一个星期一。

The function startOf('isoWeek') sets the date to the Monday of the current week. 函数startOf('isoWeek')将日期设置为当前星期的星期一。

If you want both to calculate the current , you should modify your nearestMonday like: 如果您想两者都计算current ,则应修改您的nearestMonday例如:

const nearestMonday = date => {
    const monday     = 1;
    const currentDay = date.getDay();
    const distance   = monday - currentDay;
    console.log('dist', distance);
    const newDate    = new Date(date.getTime());
    newDate.setDate(date.getDate() + distance);
    newDate.setHours(0, 0, 0, 0);
    newDate.setTime(newDate.getTime()-1000*60*newDate.getTimezoneOffset());
    return newDate;
}

Last: Sundays? 最后:星期天?

getDay() on Sunday will return a 0. Therefore, the "nearestMonday" will be the day after that. 周日的getDay()将返回0。因此,“ nearestMonday”将是其后的一天。 I haven't corrected it since I don't know if that's the desired behaviour, but noting it just for completion sake 我没有更正它,因为我不知道这是否是理想的行为,但请注意只是为了完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM