Javascript: Creating a Google Calendar RF3339 Timestamp


/ Published in: JavaScript
Save to your folder(s)

I needed a way to talk to Google Calendars. here's how I was able to create a RFC 3339 timestamp


Copy this code and paste it in your HTML
  1. /*
  2.  Internet Timestamp Generator
  3.  Copyright (c) 2009 Sebastiaan Deckers
  4.  License: GNU General Public License version 3 or later
  5. */
  6. var Timestamp = {
  7. start: function (date){
  8. date = date ? date : new Date();
  9. var offset = date.getTimezoneOffset();
  10. return this.pad(date.getFullYear(), 4)
  11. + "-" + this.pad(date.getMonth() + 1, 2)
  12. + "-" + this.pad(date.getDate(), 2)
  13. + "T" + this.pad(date.getHours(), 2)
  14. + ":" + this.pad(date.getMinutes(), 2)
  15. + ":" + this.pad(date.getSeconds(), 2)
  16. + "." + this.pad(date.getMilliseconds(), 3)
  17. + (offset > 0 ? "-" : "+")
  18. + this.pad(Math.floor(Math.abs(offset) / 60), 2)
  19. + ":" + this.pad(Math.abs(offset) % 60, 2);
  20. },
  21. pad: function (amount, width ){
  22. var padding = "";
  23. while (padding.length < width - 1 && amount < Math.pow(10, width - padding.length - 1))
  24. padding += "0";
  25. return padding + amount.toString();
  26. }
  27. }
  28.  
  29. console.log( Timestamp.start() );

URL: http://cbas.pandion.im/2009/10/generating-rfc-3339-timestamps-in.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.