Date manipulation: Difference between revisions

Lingo added
(Lingo added)
Line 1,093:
{{out}}
<pre>March 9 2009 12:30AM GMT</pre>
 
=={{header|Lingo}}==
<lang lingo>----------------------------------------
-- Returns string representation of given date object in YYYY-MM-DD hh:mm:ii format
-- @param {date} dateObj
-- @returns {string}
----------------------------------------
on dateToDateTimeString (dateObj)
str = ""
s = string(dateObj.year)
if s.length<4 then put "0000".char[1..4-s.length] before s
put s after str
s = string(dateObj.month)
if s.length<2 then s = "0"&s
put s after str
s = string(dateObj.day)
if s.length<2 then put "0" before s
put s after str
sec = dateObj.seconds
s = string(sec / 3600)
sec = sec mod 3600
if s.length<2 then put "0" before s
put s after str
s = string(sec / 60)
sec = sec mod 60
if s.length<2 then put "0" before s
put s after str
s = string(sec)
if s.length<2 then put "0" before s
put s after str
put ":" after char 12 of str
put ":" after char 10 of str
put " " after char 8 of str
put "-" after char 6 of str
put "-" after char 4 of str
return str
end</lang>
 
<lang lingo>dateStr = "March 7 2009 7:30pm EST"
 
-- parse string
month = (offset(dateStr.word[1].char[1..3], "JanFebMarAprMayJunJulAugSepOctNovDec")-1)/3 + 1
day = integer(dateStr.word[2])
year = integer(dateStr.word[3])
t = dateStr.word[4]
if t.char[t.length-1..t.length]="pm" then dh = 12
else dh = 0
t = t.char[1..t.length-2]
_player.itemDelimiter = ":"
hour = integer(t.item[1])+dh
minute = integer(t.item[2])
tz = dateStr.word[5] -- unused
 
-- original date as date object
dateObj = date(year,month,day)
dateObj.seconds = hour*3600 + minute*60
 
-- add 12 hours
sec = dateObj.seconds + 12*3600
newDateObj = dateObj + sec / 86400
newDateObj.seconds = sec mod 86400
 
-- show as YYYY-MM-DD hh:mm:ii string
put dateToDateTimeString(newDateObj)
-- "2009-03-08 07:30:00"</lang>
 
=={{header|Lua}}==
Anonymous user