Tuesday, October 13, 2009

Generating RFC 3339 or ISO 8601 Timestamps in JavaScript

I've been working on a generator for a custom Atom 1.0 feed. The Atom 1.0 specification (RFC 4287) requires Internet Timestamps (RFC 3339) which are based on ISO 8601 but these are not supported by the JavaScript Date object. Failing to find a drop-in JavaScript snippet which could generate the necessary timestamp format, I created the following function. It generates the Internet timestamp string from either the current time or a JavasScript Date object passed as an argument.

Internet Timestamp Generator
Copyright (c) 2009 Sebastiaan Deckers
License: GNU General Public License version 3 or later
var timestamp: function (date) {
var pad = function (amount, width) {
var padding = "";
while (padding.length < width - 1 && amount < Math.pow(10, width - padding.length - 1))
padding += "0";
return padding + amount.toString();
date = date ? date : new Date();
var offset = date.getTimezoneOffset();
return pad(date.getFullYear(), 4)
+ "-" + pad(date.getMonth() + 1, 2)
+ "-" + pad(date.getDate(), 2)
+ "T" + pad(date.getHours(), 2)
+ ":" + pad(date.getMinutes(), 2)
+ ":" + pad(date.getSeconds(), 2)
+ "." + pad(date.getMilliseconds(), 3)
+ (offset > 0 ? "-" : "+")
+ pad(Math.floor(Math.abs(offset) / 60), 2)
+ ":" + pad(Math.abs(offset) % 60, 2);

// "2009-10-13T13:34:52.000+02:00"

timestamp(new Date(1983, 4, 9, 11, 5, 17));
// "1983-05-09T11:05:17.000+02:00"

Hope this helps anyone.


Unknown said...


I will use it in my Chrome extension (sync with Opera Link).

Unknown said...

This is the extension:
It synchronizes Google Chrome with Google Bookmarks and Opera Link, but it is not complete yet (for example, it lacks a good user interface...)

HauNguyen said...

I get RFC Timestamp from get event list of google calendar
how to convert UTC Timestamp?

Unknown said...

Hi Sebastiaan,

I have used your work for a Gmail Reminder script I wrote. If you want to, you can view it here: http://www.myhelpfulscripts.com/email-reminder-with-tasks/

Please let me know if you want me to credit you differently.

Thanks again for your awesome work!

Eliyzia Beth said...

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.. web design agency

samharries said...

I was exceptionally satisfied to discover this site.I needed to thank you for this extraordinary read!! I certainly getting a charge out of each and every piece of it and I have you bookmarked to look at new stuff you post ISO 27001

albina N muro said...

I was reading your article and wondered if you had considered creating an ebook on this subject. Your writing would sell it fast. You have a lot of writing talent. 50 instant instagram likes

Yousufzai said...

I work for a company that is wanting to e-mail some of our media contacts from our Press Release blog posts. The main problem I am running into is finding a service that doesn't require opt-in. Does anybody have any suggestions?. nyc web designer

julie lars said...

I recently came across your blog and have been reading along. I thought I could leave my first comment. I don’t know what to say except that I have enjoyed scaning what you all have to say web design in new york

Anonymous said...
This comment has been removed by the author.
James Harris said...
This comment has been removed by the author.
sdexter said...

Thanks for your post. One other thing is when you are disposing your property all on your own, one of the problems you need to be aware about upfront is just how to deal with house inspection accounts. As a FSBO supplier, the key about successfully shifting your property in addition to saving money in real estate agent commission rates is knowledge. The more you realize, the smoother your sales effort might be. One area in which this is particularly important is inspection reports. brand identity design agency

william locas said...

Hey! Do you know if they make any plugins to help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results. If you know of any please share. Thanks! interface design agency san francisco

julie lars said...

The new Zune browser is surprisingly good, but not as good as the iPod’s. It works well, but isn’t as fast as Safari, and has a clunkier interface. If you occasionally plan on using the web browser that’s not an issue, but if you’re planning to browse the web alot from your PMP then the iPod’s larger screen and better browser may be important. iphone mockup