javascript中的日期和时区计算(GMT转换)

时间:2013-10-18 11:29:36

标签: javascript validation

在我的网页上,我可以选择“查看上周”,“查看上个月”和“查看90天”。

i)当用户点击查看上周时,我需要获取开始日期和结束日期。即

结束日期:当地时间点击“查看上周”时的当前日期。

开始日期:如果是“查看上周”,则开始日期是7天第1天的日期。

ii)与上述“查看上个月”类似,但开始日期是30天第一天的日期。

iii)对于“查看90天”,开始日期应为90天第一天的日期。

当前时区并转换为GMT:当前点击“查看上周”,“查看上个月”,“查看90天”并转换为GMT时区时的当前时区。

在Javascript中,我需要根据当地时间计算这些日期和GMT时区,然后将其发送给我的服务。

我对此毫无头绪。请求帮助。感谢。

2 个答案:

答案 0 :(得分:0)

假设您需要字符串而不是Epoch等的毫秒或秒(您尚未指定),请参阅Date

的Javascript

var today = new Date();

/*
i) When the user clicks on View Last Week , I need to get the start date and end date. i.e.

end date : current date at his local time when he clicked on 'View last week'.

start date : if it is 'View last week', the start date is the date of the 1 st day of the 7 days.
*/

console.log({
    start: new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000).toString(),
    end: today.toString()
});

/*
ii) Similarly as above for 'View last month' except that the start date is the date of the the first day of the 30 days.
*/

console.log({
    start: new Date(today.getTime() - 30 * 24 * 60 * 60 * 1000).toString(),
    end: today.toString()
});

/*
iii) For 'View 90 days', the start date should be the date of the first day of the 90 days.
*/

console.log({
    start: new Date(today.getTime() - 90 * 24 * 60 * 60 * 1000).toString(),
    end: today.toString()
});

/*
Current time zone and convert to GMT: his current local time zone when he clicked on 'View last week','View last month','View 90 days' which is converted to GMT timezone.

In Javascript,I need to calculate these dates and the GMT timezone based on his local time and send it to my service .
*/

function plz(number, length) {
    var output = number.toString();

    while (output.length < length) {
        output = "0" + output;
    }

    return output;
}

var dayName = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
    monthName = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

console.log({
    today_local: today.toString(),
    today_utc: dayName[today.getUTCDay()] + " " + monthName[today.getUTCMonth()] + " " + today.getUTCDate() + " " + today.getUTCFullYear() + " " + plz(today.getUTCHours(), 2) + ":" + plz(today.getUTCMinutes(), 2) + ":" + plz(today.getUTCSeconds(), 2) + " GMT"
});

/*
or if a custom format not required, can use RFC-1123 formatted date stamp
*/

console.log({
    today_local: today.toString(),
    today_utc: today.toUTCString()
});

输出

Object {start: "Fri Oct 11 2013 15:21:34 GMT+0200 (CEST)", end: "Fri Oct 18 2013 15:21:34 GMT+0200 (CEST)"}
Object {start: "Wed Sep 18 2013 15:21:34 GMT+0200 (CEST)", end: "Fri Oct 18 2013 15:21:34 GMT+0200 (CEST)"}
Object {start: "Sat Jul 20 2013 15:21:34 GMT+0200 (CEST)", end: "Fri Oct 18 2013 15:21:34 GMT+0200 (CEST)"}
Object {today_local: "Fri Oct 18 2013 15:21:34 GMT+0200 (CEST)", today_utc: "Fri Oct 18 2013 13:21:34 GMT"} 
Object {today_local: "Fri Oct 18 2013 15:29:59 GMT+0200 (CEST)", today_utc: "Fri, 18 Oct 2013 13:29:59 GMT"} 

jsFiddle

答案 1 :(得分:0)

使用Moment.js您的任务应该非常简单。

例如,要获取上周的开始和结束日期,您可以执行以下操作:

function getLastWeekBounds() {
  var lastWeek = moment().subtract('week', 1);
  return {
    start: lastWeek.startOf('week').toDate(),
    // => Sun Oct 06 2013 00:00:00 GMT-0600 (MDT)
    end:   lastWeek.endOf('week').toDate()
    // => Sat Oct 12 2013 23:59:59 GMT-0600 (MDT)
  };
}

您也可以使用该库以一致的方式work with timezones