Date manipulation: Difference between revisions

J
(→‎Time class: removed require rubygems)
(J)
Line 792:
= Sunday, March 8, 2009 12:30 pm UTC
= Sunday, March 8, 2009 9:00 am NST</pre>
 
=={{header|J}}==
 
A natural mechanism for representing dates in J is what J's documentation refers to as a "timestamp" -- a list of six numbers in ISO 8601 order (year, month, date, hour, minute, second). An alternate representation uses a single number specifying the number of milliseconds since January 1, 1800.
 
With that in mind:
 
<lang J>require'dates'
months=: <;._2 tolower 0 :0
January
February
March
April
May
June
July
August
September
October
November
December
)
 
numbers=: _".' '"_`(1 I.@:-e.&(":i.10)@])`]}~
words=: [:;:@tolower' '"_`(I.@(tolower = toupper)@])`]}~
getyear=: >./@numbers
getmonth=: 1 + months <./@i. words
getday=: {.@(numbers -. getyear)
gethour=: (2 { numbers) + 12 * (<'pm') e. words
getminsec=: 2 {. 3}. numbers
 
getts=: getyear, getmonth, getday, gethour, getminsec
timeadd=: 1&tsrep@+&tsrep
deltaT=: (1 tsrep 0)&([ + -@#@[ {. ])</lang>
 
This parser assumes that numeric date information appears to the left of time information, and that time zone may be ignored. (Alternate date representations are straightforward to implement but turn this into a somewhat open-ended problem).
 
Note that J's tsrep library routine converts from timestamp to milliseconds and 1 tsrep coverts from milliseconds to timestamp.
 
Example use:
 
<lang J> (deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST'
2009 3 8 7 30 0
timestamp (deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST'
08 Mar 2009 07:30:00
isotimestamp (deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST'
2009-03-08 07:30:00.000</lang>
 
Note that the isotimestamp representation uses a space instead of a 'T' to separate date and time.
 
=={{header|Java}}==
Line 814 ⟶ 863:
or using <tt>System.out.println(date);</tt> as the last line:
<pre>Sun Mar 08 08:30:00 EDT 2009</pre>
 
 
=={{header|JavaScript}}==
6,962

edits