简体   繁体   中英

Date and Timezone calculations (GMT conversion) in javascript

In my web page, I have the options 'View last week','View last month' and 'View 90 days'.

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

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.

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.

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

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 .

I am clueless on this. Request help. Thanks.

Assuming that you want strings and not milliseconds or seconds from Epoch etc. (you haven't specified), see 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()
});

Output

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

Using Moment.js your tasks should be pretty straightforward.

For example, to get the start and end dates of last week you could do the following:

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

You can also work with timezones in a coherent way using that library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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