繁体   English   中英

如何在运行node.js的服务器上使用矩js和时区偏移将日期从rrule字符串转换为UTC格式?

[英]How do I convert date into UTC format from rrule string using moment js and a timezone offset on a server running node.js?

在浏览器上,我制作了一个生成规则字符串的小部件:

var rruleString = "FREQ=WEEKLY;WKST=SU;BYDAY=MO,WE,FR;BYHOUR=8;BYMINUTE=30;BYSECOND=0"

我也可以通过以下功能从浏览器获取时区偏移量:

//This is 300 for me    
var timezoneOffSet = (new Date()).getTimezoneOffset(); 

我将它们发送到运行node.js的服务器。 在服务器上,我想使用规则和时区偏移量以UTC格式生成日期列表。 这是我现在的操作方法:

var rruleOptions = RRule.parseString(rruleString);

rruleOptions.dtstart = new Date();
rruleOptions.until = new Date();
rruleOptions.until.setMonth(options.until.getMonth() + 4);

var rule = new RRule(rruleOptions);

var dates = rule.all();

//Convert the dates into moment.js objects
momentDates = dates.map(function(date){

    //This is where I think the problem is because I think moment is
    //using the server's timezone
    return moment(date).utcOffset(timezoneOffSet*-1);      
});

//Convert the moment object array into an array of strings
var dateStringArray = momentDates.map(function(date){
    //Doesn't work returns 2015-11-27T08:30:00.00Z
    //return date.toISOString(); 

    //Doesn't work returns 2015-11-27T08:30:00.00Z
    //return date.utc().toISOString(); 

    //This works returns 2015-11-27T03:30:00.00Z for one of the objects
    return date.format('YYYY-MM-DDThh:mm')+":00.00Z";  
});

似乎我应该能够使用无法使用的功能来获取UTC中的日期,但是由于某些原因它们无法使用。 我想我缺少有关如何正确使用矩的正确方法。 谁能看到我在做什么错?

注意:如果将Date用作具有多个参数的构造函数,则指定的参数表示本地时间。 如果需要UTC,请使用带有相同参数的新Date(Date.UTC(...))。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

您可能正在使用map()。 但是正在通过没有设置键的功能。 你也没有传递数组

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

我现在想通了。

当rrule生成日期(从rule.all()生成)时,日期已经处于UTC模式(如果我错了,请纠正我,但是使用上面从console.log获得的日期之一的示例是Mon Dec) 07 2015 08:30:00 GMT + 0000(UTC)似乎表明它在UTC时间。

所以这:

//Convert the dates into moment.js objects
momentDates = dates.map(function(date){

    //This is where I think the problem is because I think moment is
    //using the server's timezone
    return moment(date).utcOffset(timezoneOffSet*-1);      
});

变成这样:

momentDates = dates.map(function(date){
    return moment(date).add(timezoneOffSet, 'minutes');      
});

从客户端角度而非服务器角度来看,添加偏移量将获得UTC时间中的日期。 现在我也意识到自己以前走错了路。 我试图减去而​​不是增加偏移量。

所以最后一点:

//Convert the moment object array into an array of strings
var dateStringArray = momentDates.map(function(date){
    //Doesn't work returns 2015-11-27T08:30:00.00Z
    //return date.toISOString(); 

    //Doesn't work returns 2015-11-27T08:30:00.00Z
    //return date.utc().toISOString(); 

    //This works returns 2015-11-27T03:30:00.00Z for one of the objects
    return date.format('YYYY-MM-DDThh:mm')+":00.00Z";  
});

现在可以变成这样:

var dateStringArray = momentDates.map(function(date){
    return date.toISOString();   
});

使用上述日期,将返回以下字符串:

2015-12-07T13:30:00.000Z

正确的UTC时间!

暂无
暂无

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

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