Generate an RFC 3339 timestamp similar to Google Tasks API?

It seems like a lot of complicated answers have been given, but this works just fine, does it not?

new Date().toISOString()

The formatting is ISO so new Date().toISOString() will give you that form. Which as I'm reading might need to be shimmed:

/* use a function for the exact format desired... */
function ISODateString(d){
 function pad(n){return n<10 ? '0'+n : n}
 return d.getUTCFullYear()+'-'
      + pad(d.getUTCMonth()+1)+'-'
      + pad(d.getUTCDate())+'T'
      + pad(d.getUTCHours())+':'
      + pad(d.getUTCMinutes())+':'
      + pad(d.getUTCSeconds())+'Z'}

var d = new Date();
print(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z

Source: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date


I've found the moment.js library nice for working with time in javascript. moment().format() yields a timestamp in the format expected by the Google API for a datetime. Or, to not depend on the default format being correct for your application,

moment().format("YYYY-MM-DDTHH:mm:ssZ")

All the string options (including fractional seconds if that's what you need): http://momentjs.com/docs/#/displaying/format/