在我的网页上,我有“查看上周”、“查看上个月”和“查看90天”的选项。
i)当用户单击View Last Week时,我需要获取开始日期和结束日期。即
结束日期:他点击“查看上周”时当地时间的当前日期。
开始日期:如果是“上周查看”,则开始日期为7天中的第1天。
ii)类似于上面的“上个月查看”,只是开始日期是30天中的第一天。
iii)对于“查看90天”,开始日期应为90天的第一天的日期。
当前时区并转换为GMT:当他点击“查看上周”、“查看上个月”、“查看90天”时,他当前的本地时区将转换为GMT时区。
在Javascript中,我需要根据他的本地时间计算这些日期和GMT时区,并将其发送到我的服务。
我对此一无所知。请求帮助。谢谢。
发布于 2013-10-18 21:27:11
假设您想要字符串,而不是来自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"} 发布于 2013-10-18 22:43:27
使用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。
https://stackoverflow.com/questions/19448614
复制相似问题