faqts : Computers : Programming : Languages : JavaScript : Language Core : Dates

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

13 of 20 people (65%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How to add minutes to a date and return as a GMT string?

May 10th, 2001 10:01
jsWalter, Chris Durkin,


wt 5/10/01
I have a collection of methods that extend the Date Object.
One of them solves this problem.
ww.torres.ws/javascript
Walter
=============================
function minutesFromNow (nn, gmt) {
// desc:  adds minutes to the current date and returns as string
// nn:    integer, the number of minutes to add
// gmt:   GMT flag - set it to force "GMT"
   var dt1 = new Date();
   var dt2 = new Date(dt1.valueOf() + (nn * 60000));
   var gmtstr = (Date.toUTCString) ? dt2.toUTCString() : dt2.toGMTString
();
   return (gmt) ? gmtstr.slice(0, -4) + " GMT": gmtstr;
}
example of use:
var dt = new Date();
document.write("Today's date: " + dt.toGMTString() + "<br>");
document.write("1 hour from now: " + minutesFromNow(60) + "<br>");
// force GMT instead of UTC
document.write("1 hour from now GMT: " + minutesFromNow(60,1));