1

在我的网页中,我有“查看上周”、“查看上个月”和“查看 90 天”选项。

i) 当用户点击 View Last Week 时,我需要获取开始日期和结束日期。IE

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

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

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

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

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

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

我对此一无所知。请求帮助。谢谢。

4

2 回答 2

1

使用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)
  };
}

您还可以使用该库以连贯的方式处理时区。

于 2013-10-18T14:43:26.853 回答
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

于 2013-10-18T13:27:10.727 回答