1 // Date/W3CDTF.js -- W3C Date and Time Formats
3 Date.W3CDTF = function ( dtf ) {
5 dd.setW3CDTF = Date.W3CDTF.prototype.setW3CDTF;
6 dd.getW3CDTF = Date.W3CDTF.prototype.getW3CDTF;
7 if ( dtf ) this.setW3CDTF( dtf );
11 Date.W3CDTF.VERSION = "0.03";
13 Date.W3CDTF.prototype.setW3CDTF = function( dtf ) {
14 var sp = dtf.split( /[^0-9]/ );
17 if ( sp.length < 6 || sp.length > 8 ) return;
20 if ( sp.length == 7 ) {
21 if ( dtf.charAt( dtf.length-1 ) != "Z" ) return;
25 for( var i=0; i<sp.length; i++ ) sp[i] = sp[i]-0;
28 if ( sp[0] < 1970 || // year
29 sp[1] < 1 || sp[1] > 12 || // month
30 sp[2] < 1 || sp[2] > 31 || // day
31 sp[3] < 0 || sp[3] > 23 || // hour
32 sp[4] < 0 || sp[4] > 59 || // min
33 sp[5] < 0 || sp[5] > 60 ) { // sec
34 return; // invalid date
37 // get UTC milli-second
39 var msec = Date.UTC( sp[0], sp[1]-1, sp[2], sp[3], sp[4], sp[5] );
42 if ( sp.length == 8 ) {
43 if ( dtf.indexOf("+") < 0 ) sp[6] *= -1;
44 if ( sp[6] < -12 || sp[6] > 13 ) return; // time zone offset hour
45 if ( sp[7] < 0 || sp[7] > 59 ) return; // time zone offset min
46 msec -= (sp[6]*60+sp[7]) * 60000;
49 // set by milli-second;
50 return this.setTime( msec );
53 Date.W3CDTF.prototype.getW3CDTF = function() {
54 var year = this.getFullYear();
55 var mon = this.getMonth()+1;
56 var day = this.getDate();
57 var hour = this.getHours();
58 var min = this.getMinutes();
59 var sec = this.getSeconds();
62 var tzos = this.getTimezoneOffset();
63 var tzpm = ( tzos > 0 ) ? "-" : "+";
64 if ( tzos < 0 ) tzos *= -1;
65 var tzhour = tzos / 60;
66 var tzmin = tzos % 60;
68 // sprintf( "%02d", ... )
69 if ( mon < 10 ) mon = "0"+mon;
70 if ( day < 10 ) day = "0"+day;
71 if ( hour < 10 ) hour = "0"+hour;
72 if ( min < 10 ) min = "0"+min;
73 if ( sec < 10 ) sec = "0"+sec;
74 if ( tzhour < 10 ) tzhour = "0"+tzhour;
75 if ( tzmin < 10 ) tzmin = "0"+tzmin;
76 var dtf = year+"-"+mon+"-"+day+"T"+hour+":"+min+":"+sec+tzpm+tzhour+":"+tzmin;
84 Date.W3CDTF - W3C Date and Time Formats
88 var dd = new Date.W3CDTF(); // now
89 document.write( "getW3CDTF: "+ dd.getW3CDTF() +"\n" );
91 dd.setW3CDTF( "2005-04-23T17:20:00+09:00" );
92 document.write( "toLocaleString: "+ dd.toLocaleString() +"\n" );
96 This module understands the W3CDTF date/time format, an ISO 8601 profile,
97 defined by W3C. This format as the native date format of RSS 1.0.
98 It can be used to parse these formats in order to create the appropriate objects.
104 This constructor method creates a new Date object which has
105 following methods in addition to Date's all native methods.
107 =head2 setW3CDTF( "2006-02-15T19:40:00Z" )
109 This method parse a W3CDTF datetime string and sets it.
113 This method returns a W3CDTF datetime string.
114 Its timezone is always local timezone configured on OS.
118 http://www.w3.org/TR/NOTE-datetime
122 Yusuke Kawasaki http://www.kawa.net/
124 =head1 COPYRIGHT AND LICENSE
126 Copyright (c) 2005-2006 Yusuke Kawasaki. All rights reserved.
127 This program is free software; you can redistribute it and/or
128 modify it under the Artistic license. Or whatever license I choose,
129 which I will do instead of keeping this documentation like it is.