Date manipulation: Difference between revisions
→{{header|langur}}
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
Langurmonkey (talk | contribs) |
||
(34 intermediate revisions by 17 users not shown) | |||
Line 12:
=={{header|11l}}==
<
print((time:strptime(‘March 7 2009 7:30pm’, format_str)
+ TimeDelta(hours' 12)).strftime(format_str))</
=={{header|Ada}}==
Line 21:
Only standard libraries are required.
<
with Ada.Calendar.Formatting;
with Ada.Calendar.Time_Zones;
Line 117:
Put_Line ("T1 => " & Img (T1, EST) & " = " & Img (T1, Lisbon));
Put_Line ("T2 => " & Img (T2, EST) & " = " & Img (T2, Lisbon));
end;</
Result:
Line 127:
=={{header|AppleScript}}==
AppleScript has a built-in date class and can coerce a string to a date automatically. It also has reserved constants such as <code>hours</code> which are defined in the unit of seconds. There is no built-in support for time zones.
<
return (date x) + 12 * hours</
Result is:
<
The above is problematical in that:
Line 140:
However, AppleScript can run shell scripts and, more recently, access some of the system's Objective-C API through its hybrid form AppleScriptObjectiveC. So as long as the date format's known, the task is doable:
<
use framework "Foundation"
Line 161:
end dateManipulationTask
dateManipulationTask()</
{{output}}
<pre>"March 8 2009 8:30am EDT"</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">; a tiny helper, so that we aren't too repetitive
formatDate: function [dt][
to :string .format: "MMMM d yyyy h:mmtt" dt
]
initial: "March 7 2009 7:30pm EST"
; chop timezone off
initial: join.with:" " chop split.words initial
initial: to :date .format: "MMMM d yyyy h:mmtt" initial
print ["initial:" formatDate initial]
print ["after 12 hours:" formatDate after.hours:12 initial]</syntaxhighlight>
{{out}}
<pre>initial: March 7 2009 7:30PM
after 12 hours: March 8 2009 7:30AM</pre>
=={{header|AutoHotkey}}==
<
; split the given string with RegExMatch
Line 231 ⟶ 252:
Result := "12"
Return, Result
}</
{{out|Message box shows}}
<pre>Given: March 7 2009 7:30pm EST
Line 240 ⟶ 261:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f DATE_MANIPULATION.AWK
BEGIN {
Line 259 ⟶ 280:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 267 ⟶ 288:
=={{header|Batch File}}==
<
@echo off
Line 349 ⟶ 370:
echo Manipulated - %monthname% %day% %year% %hour%:%minutes%%ampm% %timezone%
exit /b
</syntaxhighlight>
The code takes 3 inputs to demonstrate the ability to deal with leap years.
Line 371 ⟶ 392:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
date$ = "March 7 2009 7:30pm EST"
Line 403 ⟶ 424:
\ ":" + RIGHT$("0"+STR$(mins%), 2) + ampm$ + " " + zone$
ENDPROC
</syntaxhighlight>
{{out}}
<pre>March 8 2009 7:30am EST
Line 411 ⟶ 432:
=={{header|C}}==
{{works with|POSIX}}
<
#include <stdlib.h>
#include <time.h>
Line 429 ⟶ 450:
return EXIT_SUCCESS;
}</
Note: <code>ctime</code> treats the date as local, so that it is like the timezone information were discarded (to see the passage to daylight saving time I must change the date into March 28... no matter the timezone specified)
==={{libheader|Gadget}}===
<p>Version 1: Rosetta Code solution</p>
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
LIB_GADGET_START
void write_human_date( const char* date, const char * time )
{
Print "%s %d %d %d:%d%s EST\n", Get_monthname( Get_month( date )-1 ),\
Get_day( date ), Get_year( date ), \
Get_hour(time),Get_minute(time), \
Get_hour(time)>12 ? "pm" : "am" ;
}
#define Sign(_N_) ( (_N_)<0? -1 : 1 )
Main
Assert( Arg_count==2, fail_input );
Get_arg_int( addhour, 1 );
Set_date_lang( EN ); // fix english language
char * date = Get_date();
char * time = Get_time();
addhour = Sign( addhour )*addhour;
write_human_date(date, time);
int adddays = ( Time2sec(time) + addhour*60*60 ) / 86400;
Get_fn_let( time, Sec2time( (Time2sec(time) + addhour*60*60 )) );
Get_fn_let( date, Date_add( date, adddays ) );
write_human_date(date, time);
Free secure date, time;
Exception( fail_input ){
Msg_red("Use:\n addhour <nHours>\n")
}
End
</syntaxhighlight>
{{out}}
<pre>
$ ./addhour 12
january 11 2023 17:10pm EST
january 12 2023 5:10am EST
$ ./addhour 24
january 11 2023 17:10pm EST
january 12 2023 17:10pm EST
$ ./addhour 48
january 11 2023 17:10pm EST
january 13 2023 17:10pm EST
$ ./addhour 72
january 11 2023 17:11pm EST
january 14 2023 17:11pm EST
$ ./addhour 0
january 11 2023 17:11pm EST
january 11 2023 17:11pm EST
$ ./addhour 27
january 11 2023 17:16pm EST
january 12 2023 20:16pm EST
$ ./addhour
Use:
addhour <nHours>
</pre>
<p>Version 2: full solution</p>
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
LIB_GADGET_START
void write_human_date( const char* date, const char * time )
{
Print "%s %d %d %d:%d%s EST\n", Get_monthname( Get_month( date )-1 ),\
Get_day( date ), Get_year( date ), \
Get_hour(time),Get_minute(time), \
Get_hour(time)>=12 ? "pm" : "am" ;
}
Main
Assert( Arg_count==2, fail_input );
Get_arg_str( cTime, 1 );
int addhour;
if ( Occurs(":", cTime) ){ // input: hh:mm:ss"
int sign = 1;
if ( cTime[0] == '-' ) {
sign=-1;
Fn_let( cTime, Substr( cTime,Str_at("-",cTime)+1,strlen(cTime) ) );
}
Assert ( Is_time_valid( cTime ), fail_time );
addhour = Time2sec( cTime ) * sign;
}else if( Occurs(",", cTime) ){ // horas>24 , minutos [0->59]
long min=0;
Stack{
addhour = Str2int( Get_token(cTime,1) );
min = Time2sec( Multi_copy( NULL, "00:", Get_token(cTime,2),NULL) );
}Stack_off;
if( addhour<0 ) {
addhour = addhour*60*60 - min;
}else{
addhour = addhour*60*60 + min;
}
}else{
addhour = Time2sec( cTime ); // asume hora simple
}
Free secure cTime;
Set_date_lang( EN ); // fix english language
char * date = Get_date();
char * time = Get_time();
int SGN = Sign(addhour);
write_human_date(date, time);
int adddays=0;
if( Is_non_neg(addhour) ){
adddays = ( Time2sec(time) + addhour ) / 86400;
Get_fn_let( time, Sec2time( (Time2sec(time) + addhour )) );
Get_fn_let( date, Date_add( date, adddays ) );
}else{
int ts = Time2sec(time), nt = addhour;
int subdays = ( Time2sec(time) + SGN*addhour ) / 86400;
if( SGN*nt > ts ){
Fn_let( time, Sec2time( 86400*subdays - SGN * (Time2sec(time) + addhour )) );
}else{
if( SGN*addhour/60/60 <= Get_hour(time) && Is_non_zero(subdays) )
subdays--;
Fn_let( time, Sec2time( (Time2sec(time) + addhour )) );
}
Get_fn_let( date, Date_add( date, SGN*subdays ) );
}
write_human_date(date, time);
Free secure date, time;
Exception( fail_input ){
Msg_yellow("Use:\n addhour <[-]nHours|[-]hh:mm[:ss]|[-]hh,mm>\n"
"e.g.\n addhour 12 (add 12 hours)\n addhour 12:15 (add 12 hours + 15 mins)\n"
" addhour -04:45 (subtract 4 hours + 45 mins)\n"
" addhour 72 (add 72 hours)\n addhour -48,3 (subtract 48 hours + 3 mins)\n"
" addhour 0,-15 (subtract 15 mins = -00:15)");
}
Exception( fail_time ){
Msg_yellowf("Bad format time: %s (valid time: hh:mm[:ss])\n",cTime );
Free secure cTime;
}
End
</syntaxhighlight>
{{out}}
<pre>
$ ./tests/addhour 12
january 12 2023 15:21pm EST
january 13 2023 3:21am EST
$ ./tests/addhour 12:15
january 12 2023 15:22pm EST
january 13 2023 3:37am EST
$ ./tests/addhour -04:45
january 12 2023 15:22pm EST
january 12 2023 10:37am EST
$ ./tests/addhour 72
january 12 2023 15:22pm EST
january 15 2023 15:22pm EST
$ ./tests/addhour -48,3
january 12 2023 15:22pm EST
january 10 2023 15:19pm EST
$ ./tests/addhour 0,-15
january 12 2023 15:22pm EST
january 12 2023 15:7pm EST
$ ./tests/addhour -15,27
january 12 2023 15:27pm EST
january 12 2023 0:0am EST
$ ./tests/addhour -15,28
january 12 2023 15:27pm EST
january 11 2023 23:59pm EST
</pre>
=={{header|C sharp|C#}}==
<
{
static void Main(string[] args)
Line 447 ⟶ 655:
Console.ReadLine();
}
}</
=={{header|C++}}==
Line 453 ⟶ 661:
compiled with g++ -lboost_date_time
<
#include <iostream>
#include <boost/date_time/local_time/local_time.hpp>
Line 520 ⟶ 728:
return 0 ;
}
</syntaxhighlight>
{{out}}
<pre>
Line 530 ⟶ 738:
=={{header|Clojure}}==
<
java.text.SimpleDateFormat)
Line 540 ⟶ 748:
long
(Date. ,)
(->> , (.format sdf ,)))))</
=={{header|COBOL}}==
Line 547 ⟶ 755:
Two parts to this example. Following the task spec using POSIX routines, and a more standardized COBOL form. COBOL 2014 uses ISO8601 date and time formats, and these formats may be more common in COBOL applications.
<
program-id. date-manipulation.
Line 668 ⟶ 876:
.
end program date-manipulation.
</syntaxhighlight>
{{out}}
Line 681 ⟶ 889:
2009-03-08/07:30:00-05:00
</pre>
=={{header|Crystal}}==
<syntaxhighlight lang="ruby">time = Time.parse("March 7 2009 7:30pm EST", "%B %-d %Y %l:%M%p", Time::Location.load("EST"))
time += 12.hours
puts time # 2009-03-08 07:30:00 -05:00
puts time.in(Time::Location.load("Europe/Berlin")) # 2009-03-08 13:30:00 +01:00
</syntaxhighlight>
=={{header|D}}==
<syntaxhighlight lang="d">
import std.stdio;
import std.format;
Line 715 ⟶ 931:
}
</syntaxhighlight>
{{out}}
<pre>
Line 723 ⟶ 939:
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">
program DateManipulation;
Line 805 ⟶ 1,021:
Readln;
end.
</syntaxhighlight>
{{out}}
Line 811 ⟶ 1,027:
=={{header|EchoLisp}}==
<
(define my-date (string->date "March 7 2009 7:30 pm EST"))
→ Sun Mar 08 2009 01:30:00 GMT+0100 (CET)
Line 821 ⟶ 1,037:
(date->string my-date)
→ "8/3/2009 13:30:00" ;; human localized, Paris time.
</syntaxhighlight>
----
Line 827 ⟶ 1,043:
=={{header|Erlang}}==
It is human readable to me.
<syntaxhighlight lang="erlang">
-module( date_manipulation ).
Line 868 ⟶ 1,084:
time_from_strings_hour( Hour, "am" ) -> erlang:list_to_integer( Hour );
time_from_strings_hour( Hour, "pm" ) -> erlang:list_to_integer( Hour ) + 12.
</syntaxhighlight>
{{out}}
<pre>
Line 876 ⟶ 1,092:
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
--Date Manipulation task from Rosetta Code wiki
--User:Lnettnay
Line 887 ⟶ 1,103:
dt = add(dt, 12, HOURS)
printf(1, "%s EST\n", {format(dt, "%B %d %Y %I:%M %p")})
</syntaxhighlight>
{{out}}
<pre>
Line 896 ⟶ 1,112:
The .NET framework does not support parsing of time zone identifiers like "EST". We have to use time zone offsets like "-5".
<
let main() =
Line 909 ⟶ 1,125:
Console.WriteLine( "12 hours later in EST : {0}", date2_est )
main()</
{{out}} (depends on locale settings):
Line 918 ⟶ 1,134:
=={{header|Factor}}==
<
combinators io kernel math math.parser sequences splitting
unicode ;
Line 939 ⟶ 1,155:
"March 7 2009 7:30pm EST" parse-date dup 12 hours time+
[ timestamp>rfc822 print ] bi@</
{{out}}
<pre>
Line 950 ⟶ 1,166:
In the expression "d + 12hr", the "12hr" defines an instance of the Duration class, interpreting the duration in nanoseconds.
<
fansh> d := DateTime.fromLocale("March 7 2009 7:30pm EST", "MMMM D YYYY h:mmaa zzz")
fansh> d
Line 958 ⟶ 1,174:
fansh> (d+12hr).toTimeZone(TimeZone("London")) // the extra credit!
2009-03-08T12:30:00Z London
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
#include "vbcompat.bi"
Line 1,046 ⟶ 1,262:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 1,059 ⟶ 1,275:
and automatically converts between timezones.
By default, output times are in the user's defined timezone.
<
### MMM dd yyyy h:mma ###
d = parseDate["March 7 2009 7:30pm EST"]
println[d + 12 hours -> Eastern]
println[d + 12 hours -> Switzerland] // Extra credit
</syntaxhighlight>
{{out}}
Line 1,073 ⟶ 1,289:
=={{header|FunL}}==
<
pattern = SimpleDateFormat( 'MMMM d yyyy h:mma zzz' )
Line 1,080 ⟶ 1,296:
println( pattern.format(later) ) // Eastern Daylight Time
pattern.setTimeZone( TimeZone.getTimeZone('America/Los_Angeles') )
println( pattern.format(later) ) // U.S. Pacific Time</
{{out}}
Line 1,091 ⟶ 1,307:
=={{header|Fōrmulæ}}==
'''Solution'''
'''Creating the initial time.''' Fōrmulæ can create a time in any time zone, however, it always displays them in the current timezone. In order to cause no confusion, in this example the current time zone has been set as America/New_York, which is EST.
[[File:Fōrmulæ - Date manipulation 01.png]]
[[File:Fōrmulæ - Date manipulation 02.png]]
'''Adding 12 hours.''' It is achieved adding to the time expression the desired number of milliseconds.
[[File:Fōrmulæ - Date manipulation 03.png]]
[[File:Fōrmulæ - Date manipulation 04.png]]
This is not a bug, the daylight saving time (in the America/New_York time zone), for 2009 started March 8 at 2:00 hrs, at which time clock is adjusted 1 hour later. It occurred between the 12 hour addition of our example. See [https://www.timeanddate.com/time/change/usa/new-york?year=2009 this page].
Note the ☀ symbol, indicating that the time is in daylight saving time (at the current time zone).
'''Showing results for other time zones.''' As it was said before, a time is always shown in current time zone, but a time can be formatted to different time zones.
If no time zone is specified, current time zone is used:
[[File:Fōrmulæ - Date manipulation 05.png]]
[[File:Fōrmulæ - Date manipulation 06.png]]
[[File:Fōrmulæ - Date manipulation 07.png]]
[[File:Fōrmulæ - Date manipulation 08.png]]
Let us use a different time zone:
[[File:Fōrmulæ - Date manipulation 09.png]]
[[File:Fōrmulæ - Date manipulation 10.png]]
[[File:Fōrmulæ - Date manipulation 11.png]]
[[File:Fōrmulæ - Date manipulation 12.png]]
Beside the time zone, a different locale can also be specified, in order to format the result in such that locale:
[[File:Fōrmulæ - Date manipulation 13.png]]
[[File:Fōrmulæ - Date manipulation 14.png]]
The components of a time expression can be obtained individually, even for a specific time zone:
[[File:Fōrmulæ - Date manipulation 15.png]]
[[File:Fōrmulæ - Date manipulation 16.png]]
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn DoIt
CFStringRef monthString, zoneString, ampmString
long month, day, year, hour, minute
CFStringRef dateString = @"March 7 2009 7:30pm EST"
DateFormatterRef df = fn DateFormatterInit
DateFormatterSetDateStyle( df, NSDateFormatterMediumStyle )
DateFormatterSetTimeStyle( df, NSDateFormatterMediumStyle )
DateFormatterSetDateFormat( df, @"MMMM d YYYY h:ma" )
CFArrayRef months = fn DateFormatterMonthSymbols( df )
CFCharacterSetRef spaceSet = fn CharacterSetWhitespaceSet
ScannerRef scanner = fn ScannerWithString( dateString )
ScannerSetCharactersToBeSkipped( scanner, fn CharacterSetWithCharactersInString(@": ") )
fn ScannerScanUpToCharactersFromSet( scanner, spaceSet, @monthString )
fn ScannerScanInteger( scanner, @day )
fn ScannerScanInteger( scanner, @year )
fn ScannerScanInteger( scanner, @hour )
fn ScannerScanInteger( scanner, @minute )
fn ScannerScanUpToCharactersFromSet( scanner, spaceSet, @ampmString )
fn ScannerScanUpToCharactersFromSet( scanner, spaceSet, @zoneString )
month = fn ArrayIndexOfObject( months, monthString ) + 1
if ( fn StringIsEqual( ampmString, @"pm" ) ) then hour += 12
DateComponentsRef comps = fn DateComponentsInit
DateComponentsSetMonth( comps, month )
DateComponentsSetDay( comps, day )
DateComponentsSetYear( comps, year )
DateComponentsSetHour( comps, hour + 12 )
DateComponentsSetMinute( comps, minute )
CFDateRef dt = fn CalendarDateFromComponents( fn CalendarCurrent, comps )
CFStringRef string = fn DateFormatterStringFromDate( df, dt )
string = fn StringByAppendingFormat( string, @" %@", zoneString )
print string
end fn
fn DoIt
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
March 8 2009 7:30am EST
</pre>
=={{header|Go}}==
<
import (
Line 1,128 ⟶ 1,445:
fmt.Println("+12 hrs in Arizona:", t.In(atz))
}
}</
{{out}}
<pre>
Line 1,140 ⟶ 1,457:
{{libheader|Joda Time|2.1}}
<
import java.text.*
Line 1,151 ⟶ 1,468:
println (dt)
println (dt.plusHours(12))
println (dt.plusHours(12).withZone(DateTimeZone.UTC))</
{{out}}
Line 1,160 ⟶ 1,477:
=={{header|Haskell}}==
<
import qualified Data.Time.Format as F
Line 1,173 ⟶ 1,490:
"%B %e %Y %l:%M%P %Z"
"March 7 2009 7:30pm EST"
t2 = P.posixSecondsToUTCTime $ 12 * 60 * 60 + P.utcTimeToPOSIXSeconds t1</
{{Out}}
<pre>2009-03-08 12:30:00 UTC</pre>
=={{header|HicEst}}==
<
CHARACTER date="March 7 2009 7:30pm EST", am_pm, result*20
Line 1,189 ⟶ 1,506:
! result = "Sun 2009-03-08 07:30"
END
</syntaxhighlight>
=={{header|Icon}} and {{header|Unicon}}==
This uses the datetime procedures from the Icon Programming Library. Several supplemental procedures were needed to normalize the date format (as the one used in the task isn't fully compatible with the library), and to better handle time zones (as the library routines don't handle part hour time zones).
<
procedure main()
Line 1,259 ⟶ 1,576:
}
return case x of { "list" : AZ ; "table" : TZ }
end</
{{libheader|Icon Programming Library}}
Line 1,276 ⟶ 1,593:
With that in mind:
<
months=: <;._2 tolower 0 :0
January
Line 1,302 ⟶ 1,619:
getts=: getyear, getmonth, getday, gethour, getminsec
timeadd=: 1&tsrep@+&tsrep
deltaT=: (1 tsrep 0)&([ + -@#@[ {. ])</
This parser assumes that numeric date information appears to the left of time information, that month name is spelled out in full and that time zone may be ignored. (Alternate date representations are straightforward to implement but turn this into a somewhat open-ended problem).
Line 1,310 ⟶ 1,627:
Example use:
<
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</
Note that the isotimestamp representation uses a space instead of a 'T' to separate date and time.
=={{header|Java}}==
<
import java.
class Main {
public static void main(String args[]) {
String dateStr = "March 7 2009 7:30pm EST";
DateTimeFormatter df = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("MMMM d yyyy h:mma zzz")
.toFormatter();
ZonedDateTime after12Hours = ZonedDateTime.parse(dateStr, df).plusHours(12);
System.out.println("Date: " + dateStr);
System.out.println("+12h: " + after12Hours.format(df));
ZonedDateTime after12HoursInCentralEuropeTime = after12Hours.withZoneSameInstant(ZoneId.of("CET"));
System.out.println("+12h (in Central Europe): " + after12HoursInCentralEuropeTime.format(df));
}
}
</syntaxhighlight>
{{out}}
<pre>
Date: March 7 2009 7:30pm EST
+12h: March 8 2009 8:30AM EDT
+12h (in Central Europe): March 8 2009 1:30PM CET
</pre>
=={{header|JavaScript}}==
Line 1,355 ⟶ 1,680:
While ECMA-262 Ed 5 specifies a <code>Date.parse</code> method, it is not widely supported (2011) and parsing of strings other than the format specified are implementation dependent. Since the test string doesn't conform to the standard, it must be manually parsed.
<
// Get the parts of the date string
Line 1,395 ⟶ 1,720:
'Input: ' + inputDateString + '\n' +
'+12hrs in local time: ' + add12hours(inputDateString)
);</
=={{header|jq}}==
{{works with|jq|version with mktime}}
<
| strptime("%B %d %Y %I:%M%p %Z")
| .[3] += 12
| mktime | strftime("%B %d %Y %I:%M%p %Z") </
{{out}}
<
=={{header|Julia}}==
===without TimeZones library===
<
function main()
Line 1,421 ⟶ 1,746:
main()
</
<pre>March 08 2009 07:30</pre>
===With TimeZones.jl===
<
function testdateparse()
Line 1,438 ⟶ 1,763:
testdateparse()
</
<pre>March 07 2009 07:30AM +05:00</pre>
=={{header|Kotlin}}==
<
import java.text.SimpleDateFormat
Line 1,460 ⟶ 1,785:
cal.timeZone = TimeZone.getTimeZone("MST")
println(fmt.format(cal))
}</
{{out}}
Line 1,466 ⟶ 1,791:
March 08 2009 7:30am EST
March 08 2009 5:30am MST
</pre>
=={{header|langur}}==
Langur currently uses the Go time package. Testing with Go 1.14.1 on Linux, the time package doesn't seem to parse "EST" correctly, and it seems to fail silently. Given these conditions, I use "-05:00" instead of "EST" in the input string.
<syntaxhighlight lang="langur">val .input = "March 7 2009 7:30pm -05:00"
val .iformat = "January 2 2006 3:04pm -07:00"
val .oformat = "January 2 2006 3:04pm MST"
val .d1 = datetime .input, .iformat
val .d2 = .d1 + dr/PT12H/
val .d3 = datetime .d2, "US/Arizona"
val .d4 = datetime .d2, zls
val .d5 = datetime .d2, "Z"
val .d6 = datetime .d2, "+02:30"
val .d7 = datetime .d2, "EST"
writeln "input string: ", .input
writeln "input format string: ", .iformat
writeln "output format string: ", .oformat
writeln()
writeln $"original: \.d1; (\.d1:dt oformat;)"
writeln $"+12 hours: \.d2; (\.d2:dt oformat;)"
writeln $"in Arizona: \.d3; (\.d3:dt oformat;)"
writeln $"in local time zone: \.d4; (\.d4:dt oformat;)"
writeln $"in UTC: \.d5; (\.d5:dt oformat;)"
writeln $"+02:30 time zone: \.d6; (\.d6:dt oformat;)"
writeln $"in EST: \.d7; (\.d7:dt oformat;)"</syntaxhighlight>
{{out}}
<pre>input string: March 7 2009 7:30pm -05:00
input format string: January 2 2006 3:04pm -07:00
output format string: January 2 2006 3:04pm MST
original: 2009-03-07T19:30:00-05:00 (March 7 2009 7:30pm -0500)
+12 hours: 2009-03-08T07:30:00-05:00 (March 8 2009 7:30am -0500)
in Arizona: 2009-03-08T05:30:00-07:00 (March 8 2009 5:30am MST)
in local time zone: 2009-03-08T07:30:00-05:00 (March 8 2009 7:30am CDT)
in UTC: 2009-03-08T12:30:00Z (March 8 2009 12:30pm UTC)
+02:30 time zone: 2009-03-08T15:00:00+02:30 (March 8 2009 3:00pm +0230)
in EST: 2009-03-08T07:30:00-05:00 (March 8 2009 7:30am EST)
</pre>
=={{header|Lasso}}==
<
#date->add(-hour = 24)
#date->timezone = 'GMT'</
{{out}}
Line 1,477 ⟶ 1,844:
=={{header|Lingo}}==
<
-- Returns string representation of given date object in YYYY-MM-DD hh:mm:ii format
-- @param {date} dateObj
Line 1,511 ⟶ 1,878:
put "-" after char 4 of str
return str
end</
<
-- parse string
Line 1,539 ⟶ 1,906:
-- show as YYYY-MM-DD hh:mm:ii string
put dateToDateTimeString(newDateObj)
-- "2009-03-08 07:30:00"</
=={{header|Lua}}==
The following solution is quite ugly, but unfortunately there is not anything like 'strptime'-function in Lua.
<
str = string.lower( "March 7 2009 7:30pm EST" )
Line 1,570 ⟶ 1,937:
print( os.date( "%c", os.time{ year=year, month=month, day=day, hour=hour, min=min, sec=0 } + 12 * 3600 ) )
</syntaxhighlight>
{{out}}
<pre>Sun Mar 8 07:30:00 2009</pre>
=={{header|Maple}}==
<
local dt, zone;
local months := ["January","February","March","April","May","June","July","August","September","October","November","December"];
Line 1,590 ⟶ 1,957:
printf(TimeZone(dt));
end proc;
</syntaxhighlight>
{{Out|Usage}}
<syntaxhighlight lang="text">twelve_hours("March 7 2009 7:30pm EST");
twelve_hours("March 2 2009 0:10am WET");
twelve_hours("March 2 2009 6:30am AST");</
{{Out|Output}}
<pre>
Line 1,603 ⟶ 1,970:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
DateString[DatePlus[dstr, {12, "Hour"}], {"DayName", " ", "MonthName", " ", "Day", " ", "Year", " ", "Hour24", ":", "Minute", "AMPM"}]</
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">import "dateTime"
import "stringUtil"
months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
]
date = "March 7 2009 7:30pm EST"
print "Original date/time : " + date
// change the date to standard format
items = date.split
month = months.indexOf(items[0]) + 1
day = items[1]
year = items[2]
time = items[3]
hour = time.split(":")[0].val
minute = time.split(":")[1][0:2]
pm = time.endsWith("pm")
if pm then hour = hour + 12
time = hour + ":" + minute
zone = items[4]
date = year + "-" + month + "-" + day + " " + time
// add 12 hours and display in original format
dval = dateTime.val(date) + 12*60*60
dfmt = "MMMM d yyyy h:mmtt"
date2 = dateTime.str(dval, dfmt) + " " + zone
print "12 hours later : " + date2
// change from EST to MST (2 hours earlier)
date3 = dateTime.str(dval - 2*60*60, dfmt) + " MST"
print "Adjusted to MST : " + date3</syntaxhighlight>
{{out}}
<pre>Original date/time : March 7 2009 7:30pm EST
12 hours later : March 8 2009 7:30am EST
Adjusted to MST : March 8 2009 5:30am MST
</pre>
=={{header|mIRC Scripting Language}}==
<
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols binary
Line 1,639 ⟶ 2,048:
end
return
</syntaxhighlight>
{{out}}
<pre>
Line 1,647 ⟶ 2,056:
=={{header|Nim}}==
Timezones as “EST” are not recognized by the “times” module which follows ISO 8601 standard. So we replace it by a normalized timezone: -05:00. It would be easy to build a table to map these timezone identifiers to normalized timezones (being careful however regarding DST).
We output the dates built after parsing in UTC. The module allows also to output in local time.
<syntaxhighlight lang="nim">import times
const Date = "March 7 2009 7:30pm EST"
echo "Original date is: ", Date
var dt = Date.replace("EST", "-05:00").parse("MMMM d yyyy h:mmtt zzz")
echo "Original date in UTC is: ", dt.utc().format("MMMM d yyyy h:mmtt zzz")
dt = dt + initDuration(hours = 12)
echo "Date 12 hours later is: ", dt.utc().format("MMMM d yyyy h:mmtt zzz")</syntaxhighlight>
{{out}}
<pre>
Original date in UTC is: March 8 2009 12:30AM Z
Date 12 hours later is: March 8 2009 12:30PM Z</pre>
=={{header|ooRexx}}==
===version 1===
<syntaxhighlight lang="oorexx">
sampleDate = 'March 7 2009 7:30pm EST'
Line 1,785 ⟶ 2,206:
*/
return
</syntaxhighlight>
===version 2===
This example is written using the Open Object Rexx dialect to take advantage of the <code>DateTime</code> built–in class.
<
sampleDate = 'March 7 2009 7:30pm EST'
Line 2,006 ⟶ 2,427:
__ENDD__
*/
</syntaxhighlight>
{{out}}
<pre>
Line 2,033 ⟶ 2,454:
We use Mountain Daylight Time for output.
<
use DateTime::Format::Strptime 'strptime';
use feature 'say';
Line 2,043 ⟶ 2,464:
->add(hours => 12)
->set_time_zone('America/Edmonton')
->format_cldr('MMMM d yyyy h:mma zzz');</
If we're given an ambiguous timezone like 'EST' for input, we can handle this by changing it to the unambiguous Olson timezone id. This ensures daylight savings is correctly handled (which is especially tricky here, since March 7/8 is the DST rollover, and times jump ahead skipping an hour)
Line 2,051 ⟶ 2,472:
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins<span style="color: #0000FF;">\<span style="color: #004080;">timedate<span style="color: #0000FF;">.<span style="color: #000000;">e</span>
<span style="color: #7060A8;">set_timedate_formats<span style="color: #0000FF;">(<span style="color: #0000FF;">{<span style="color: #008000;">"Mmmm d yyyy h:mmpm tz"<span style="color: #0000FF;">}<span style="color: #0000FF;">)</span>
<span style="color: #004080;">timedate</span> <span style="color: #000000;">td</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">parse_date_string<span style="color: #0000FF;">(<span style="color: #008000;">"March 7 2009 7:30pm EST"<span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">twelvehours</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">timedelta<span style="color: #0000FF;">(<span style="color: #000000;">hours<span style="color: #0000FF;">:=<span style="color: #000000;">12<span style="color: #0000FF;">)</span>
<span style="color: #000000;">td</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">adjust_timedate<span style="color: #0000FF;">(<span style="color: #000000;">td<span style="color: #0000FF;">,<span style="color: #000000;">twelvehours<span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?<span style="color: #7060A8;">format_timedate<span style="color: #0000FF;">(<span style="color: #000000;">td<span style="color: #0000FF;">)</span>
<span style="color: #000000;">td</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">change_timezone<span style="color: #0000FF;">(<span style="color: #000000;">td<span style="color: #0000FF;">,<span style="color: #008000;">"ACDT"<span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- extra credit</span>
<span style="color: #0000FF;">?<span style="color: #7060A8;">format_timedate<span style="color: #0000FF;">(<span style="color: #000000;">td<span style="color: #0000FF;">)</span>
<span style="color: #000000;">td</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">adjust_timedate<span style="color: #0000FF;">(<span style="color: #000000;">td<span style="color: #0000FF;">,<span style="color: #7060A8;">timedelta<span style="color: #0000FF;">(<span style="color: #000000;">days<span style="color: #0000FF;">:=<span style="color: #000000;">31<span style="color: #0000FF;">*<span style="color: #000000;">4<span style="color: #0000FF;">)<span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?<span style="color: #7060A8;">format_timedate<span style="color: #0000FF;">(<span style="color: #000000;">td<span style="color: #0000FF;">)
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,069 ⟶ 2,493:
=={{header|PHP}}==
<
$time = new DateTime('March 7 2009 7:30pm EST');
$time->modify('+12 hours');
echo $time->format('c');
?></
=={{header|PicoLisp}}==
<
(use (@Mon @Day @Year @Time @Zone)
(and
Line 2,094 ⟶ 2,518:
(dec '@Time 86400)
(inc 'Date) )
(pack (dat$ Date "-") " " (tim$ @Time T) " " @Zone) ) ) ) )</
=={{header|Pike}}==
<
Result: "Saturday, 7 March 2009 12:30:00"</
=={{header|PL/I}}==
<
/* different formats, but not that particular one. For any of the */
/* standard formats, the following date manipulation will add */
Line 2,108 ⟶ 2,532:
seconds = SECS(DATETIME());
seconds = seconds + 12*60*60;
put list (SECSTODATE(seconds));</
=={{header|PowerShell}}==
The .NET framework does not support parsing of time zone identifiers like "EST". We have to use time zone offsets like "-5".
<
write-host $date
write-host $date.AddHours(12)
write-host [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($date.AddHours(12),"Vladivostok Standard Time")</
{{out}} (depends on user regional settings):
<pre>domingo, 08 de marzo de 2009 1:30:00
Line 2,122 ⟶ 2,546:
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">
EnableExplicit
Line 2,221 ⟶ 2,645:
CloseConsole()
EndIf
</syntaxhighlight>
{{out}}
Line 2,233 ⟶ 2,657:
I don't do anything with timezone here, but it is possible.
<
def mt():
Line 2,244 ⟶ 2,668:
print datime2.strftime("%B %d %Y %I:%M%p %Z") + datime1[-3:]
mt()</
=={{header|R}}==
<
isotime <- ISOdatetime(1900 + time$year, time$mon, time$mday,
time$hour, time$min, time$sec, "EST") # "2009-02-07 19:30:00 EST"
twelvehourslater <- isotime + 12 * 60 * 60 # "2009-02-08 07:30:00 EST"
timeincentraleurope <- format(isotime, tz="CET", usetz=TRUE) #"2009-02-08 01:30:00 CET"</
=={{header|Racket}}==
The solution below ignores the time zone.
<
#lang racket
(require srfi/19)
Line 2,273 ⟶ 2,697:
12hours))
"~a ~d ~b ~Y ~H:~M")
</syntaxhighlight>
{{out}}
<
"Sun 08 Mar 2009 07:30"
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
Raku comes with a built-in DateTime type to support most aspects of standard civic time calculation
that are not dependent on cultural idiosyncrasies. <br>
Unfortunately, Raku does not yet have a date parsing module – mostly due to a reticence to inflict Western cultural imperialism on other cultures... or maybe just due to laziness. But that just gives us another opportunity to demonstrate the built-in grammar support.
<syntaxhighlight lang="raku" line>my @month = <January February March April May June July August September October November December>;
my %month = flat (@month Z=> 1..12), (@month».substr(0,3) Z=> 1..12), 'Sept' => 9;
grammar US-DateTime {
Line 2,338 ⟶ 2,761:
$dt = $dt.later(hours => 12);
say "12 hours later,
say "12 hours later,
{{out}}
<pre>12 hours later,
12 hours later,
=={{header|REBOL}}==
<
Title: "Date Manipulation"
URL: http://rosettacode.org/wiki/Date_Manipulation
Line 2,369 ⟶ 2,792:
print 12:00 + read-time "March 7 2009 7:30pm EST"
</syntaxhighlight>
{{out}}
Line 2,375 ⟶ 2,798:
=={{header|Red}}==
<syntaxhighlight lang="red">
d: 07-Mar-2009/19:30 + 12:00
print d
Line 2,381 ⟶ 2,804:
d/timezone: 1
print d
8-Mar-2009/8:30:00+01:00</
=={{header|REXX}}==
This version only works with REXXes that support the '''date''' and '''time''' extended functions.
<
aDate = 'March 7 2009 7:30pm EST' /*the original or base date to be used.*/
Line 2,400 ⟶ 2,823:
say aDate ' + 12 hours ───► ' ndate ntime tz /*display the new timestamp to console.*/
/*stick a fork in it, we're all done. */</
'''output'''
<pre>
Line 2,407 ⟶ 2,830:
=={{header|Ring}}==
<
# Project : Date manipulation
Line 2,434 ⟶ 2,857:
see "Original - " + dateorigin + nl
see "Manipulated - " + d + " " + t1 + ap + nl
</syntaxhighlight>
Output:
<pre>
Original - March 7 2009 7:30pm EST
Manipulated - 08/03/2009 7:30am
</pre>
=={{header|RPL}}==
{{works with|HP|48}}
RPL time format does not handle time zones.
≪ "@" + → in
≪ { }
1 in SIZE '''FOR''' j
in j DUP SUB
'''CASE'''
"0123456789" OVER POS '''THEN''' STR→ '''END'''
DUP "a" ≥ OVER "z" ≤ AND '''THEN''' NUM 32 - CHR '''END'''
DUP "A" < OVER "Z" > OR '''THEN''' DROP ‘x’ '''END'''
'''END'''
'''IF''' OVER TYPE 5 ≠ '''THEN'''
'''IF''' DUP2 TYPE SWAP TYPE == '''THEN'''
'''IF''' DUP TYPE 2 ≠ '''THEN''' SWAP 10 * '''END''' +
'''ELSE'''
ROT ROT + SWAP
'''IF''' DUP ‘x’ SAME '''THEN''' DROP '''END'''
'''END END'''
'''NEXT'''
≫ ≫ '<span style="color:blue">ALPHANUM</span>' STO
≪ <span style="color:blue">ALPHANUM</span> → date
≪ { "JAN" "FEB" "MAR" "APR" "MAY" "JUN" "JUL" "AUG" "SEP" "OCT" "NOV" "DEC" }
date 1 GET 1 3 SUB POS 100 /
date 2 GET +
date 3 GET 1E6 / +
date 4 GET
date 5 GET 100 / +
'''IF''' date 6 GET "PM" == '''THEN''' 12 + '''END'''
≫ ≫ '<span style="color:blue">→DATIME</span>' STO
≪ <span style="color:blue">→DATIME</span> 1 +
'''IF''' DUP 24 > '''THEN''' 24 - SWAP 1 DATE+ SWAP '''END'''
TSTR
≫ '<span style="color:blue">TASK1</span>' STO
"March 7 2009 7:30pm EST" <span style="color:blue">TASK1</span>
{{out}}
<pre>
1: "SUN 08.03.09 07:30:00"
</pre>
Line 2,446 ⟶ 2,912:
{{libheader|ActiveSupport}}
<
d = "March 7 2009 7:30pm EST"
t = Time.parse(d)
Line 2,463 ⟶ 2,929:
# or, remote = new.in_time_zone('Beijing')
puts remote.rfc2822
puts remote.zone</
{{out}}
<pre>Sat, 07 Mar 2009 19:30:00 -0500
Line 2,473 ⟶ 2,939:
Using [[:Category:ActiveSupport|ActiveSupport]], we can add 12 hours with any of:
<
new = t.in(12.hours)
new = t.advance(:hours => 12)</
===DateTime class===
<
puts d1 = DateTime.parse("March 7 2009 7:30pm EST")
# d1 + 1 would add a day, so add half a day:
puts d2 = d1 + 1/2r # 1/2r is a rational; 0.5 would also work
puts d3 = d2.new_offset('+09:00')</
{{out}}
<pre>
Line 2,492 ⟶ 2,958:
=={{header|Run BASIC}}==
<
monthName$ = "January February March April May June July August September October November December"
Line 2,509 ⟶ 2,975:
ap$ = "am"
end if
print date$(d);" ";t1$;ap$</
=={{header|Rust}}==
<syntaxhighlight lang="rust">
use chrono::prelude::*;
use chrono::Duration;
fn main() {
// Chrono allows parsing time zone abbreviations like "EST", but
// their meaning is ignored due to a lack of standardization.
//
// This solution compromises by augmenting the parsed datetime
// with the timezone using the IANA abbreviation.
let ndt =
NaiveDateTime::parse_from_str("March 7 2009 7:30pm EST", "%B %e %Y %l:%M%P %Z").unwrap();
// add TZ manually
let dt = chrono_tz::EST.from_local_datetime(&ndt).unwrap();
println!("Date parsed: {:?}", dt);
let new_date = dt + Duration::hours(12);
println!("+12 hrs in EST: {:?}", new_date);
println!(
"+12 hrs in CET: {:?}",
new_date.with_timezone(&chrono_tz::CET)
);
}
</syntaxhighlight>
{{out}}
<pre>
Date parsed: 2009-03-07T19:30:00EST
+12 hrs in EST: 2009-03-08T07:30:00EST
+12 hrs in CET: 2009-03-08T13:30:00CET
</pre>
=={{header|Scala}}==
<
import java.util.{Calendar, Locale, TimeZone}
Line 2,528 ⟶ 3,027:
println(df.format(c.getTime))
}
}</
{{out}}
<pre>March 8 2009 8:30AM EDT
March 8 2009 12:30PM GMT</pre>
=={{header|Seed7}}==
Line 2,541 ⟶ 3,041:
In the example below EST is replaced with UTC-05.
<
include "time.s7i";
include "duration.s7i";
Line 2,586 ⟶ 3,086:
aTime := toUTC(aTime);
writeln("In UTC: " <& aTime);
end func;</
{{out}}
Line 2,596 ⟶ 3,096:
=={{header|SenseTalk}}==
<
insert "[month name] [day] [year] [hour12]:[min][pm] [timeZoneID]" into the timeInputFormat
put date + 12 hours
</syntaxhighlight>
Output:
<
=={{header|Sidef}}==
{{trans|Perl}}
<
var input = 'March 7 2009 7:30pm EST'
Line 2,614 ⟶ 3,114:
.add(hours => 12) \
.set_time_zone('America/Edmonton') \
.format_cldr('MMMM d yyyy h:mma zzz')</
{{out}}
<pre>March 8 2009 6:30AM MDT</pre>
Line 2,627 ⟶ 3,127:
The code also fails when adding a duration that "jumps" beyond two DST changes (e.g from EST to EDT and EST again); (it could be partially fixed by considering intervals instead of single date, and adding a fourth element to link to the "new" timezone abbreviation)
<
setYear: aNum [ year := aNum ]
].
Line 2,676 ⟶ 3,176:
dateTime: aDT [ dateAndTime := aDT ]
].</
Usage example (note: the code is rather rigid, so not all operations possible on DateTime are possible on DateTimeTZ).
<
s := 'March 7 2009 7:30pm EST'.
Line 2,708 ⟶ 3,208:
}) displayNl.
(dt dateTime) asUTC displayNl.</
{{out}} (note that EST should be EDT):
Line 2,716 ⟶ 3,216:
=={{header|SQL}}==
{{works with|Oracle}}
<
-- March 7 2009 7:30pm EST
Line 2,749 ⟶ 3,249:
at time zone 'US/Arizona' plus_12_nodst
from dual;
</syntaxhighlight>
<pre>
Line 2,767 ⟶ 3,267:
08-MAR-09 05.30.00.000000000 AM US/ARIZONA
</pre>
=={{header|Standard ML}}==
<syntaxhighlight lang="standard ml">
val smltime= fn input => (* parse given format *)
let
val mth::day::year::itime::t = String.fields Char.isSpace input ;
val tmp = String.fields (fn x=> x= #":") itime;
val h = (valOf(Int.fromString (hd tmp) )) + (if String.isSuffix "pm" (hd(tl tmp)) then 12 else 0 ) ;
val ms = (String.extract (hd (tl tmp), 0 ,SOME 2))^":00" ;
val mth = String.extract (mth,0,SOME 3)
in
(* Sat is a dummy *)
Date.fromString ("Sat "^mth ^" " ^ (StringCvt.padLeft #"0" 2 day) ^ " "^(StringCvt.padLeft #"0" 2 (Int.toString h))^":" ^ ms^" "^ year )
end;
local
val date2real = Time.toReal o Date.toTime o valOf
val onehour = date2real ( Date.fromString "Mon Jan 01 23:59:59 1973" ) - ( date2real ( Date.fromString "Mon Jan 01 22:59:59 1973" )) ;
in
val hoursFrom = fn hours => fn from =>
(Date.fromTimeLocal o Time.fromReal)( ( date2real from) + hours * onehour );
end;
</syntaxhighlight>
output
<syntaxhighlight lang="standard ml">
hoursFrom 12.0 ( smltime "March 7 2009 7:30pm EST" ) ;
val it = Sun Mar 08 07:30:00 2009: Date.date
</syntaxhighlight>
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd yyyy hh:mma zzz"
guard let date = formatter.date(from: "March 7 2009 7:30pm EST") else {
fatalError()
}
print(formatter.string(from: date))
print(formatter.string(from: date + 60 * 60 * 12))</syntaxhighlight>
{{out}}
<pre>March 07 2009 07:30PM EST
March 08 2009 08:30AM EDT</pre>
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<
set epoch [clock scan $date -format "%B %d %Y %I:%M%p %z"]
set later [clock add $epoch 12 hours]
puts [clock format $later] ;# Sun Mar 08 08:30:00 EDT 2009
puts [clock format $later -timezone :Asia/Shanghai] ;# Sun Mar 08 20:30:00 CST 2009</
Note the transition into daylight savings time in the interval (in the Eastern timezone).
Line 2,780 ⟶ 3,330:
=={{header|UNIX Shell}}==
requires GNU date
<
date -d @$epoch
TZ=Asia/Shanghai date -d @$epoch</
{{out}}
<pre>Sun Mar 8 08:30:00 EDT 2009
Sun Mar 8 20:30:00 CST 2009</pre>
A version that works with the BSD/macOS version of date(1):
<syntaxhighlight lang="bash">epoch=$(( $(date -j -f '%B %d %Y %l:%M%p %Z' 'March 7 2009 7:30pm EST' +%s) + 43200 ))
date -r $epoch
TZ=Australia/Perth date -r $epoch</syntaxhighlight>
{{out}}
<pre>Sun Mar 8 08:30:55 EDT 2009
Sun Mar 8 21:30:55 AWDT 2009</pre>
=={{header|Wren}}==
{{libheader|Wren-date}}
<syntaxhighlight lang="wren">import "./date" for Date
var fmt = "mmmm| |d| |yyyy| |H|:|MM|am| |zz|"
var d = Date.parse("March 7 2009 7:30pm EST", fmt)
Date.default = fmt
System.print("Original date/time : %(d)")
d = d.addHours(12)
System.print("12 hours later : %(d)")
// Adjust to MST say
d = d.adjustTime("MST")
System.print("Adjusted to MST : %(d)")</syntaxhighlight>
{{out}}
<pre>
Original date/time : March 7 2009 7:30pm EST
12 hours later : March 8 2009 7:30am EST
Adjusted to MST : March 8 2009 5:30am MST
</pre>
=={{header|zkl}}==
The iso8601 library offers additional Time/Date support but using the built in stuff:
<
fcn add12h(dt){
re:=RegExp(0'|(\w+)\s+(\d+)\s+(\d+)\ +(.+)\s|);
Line 2,800 ⟶ 3,380:
Y,M,D, h,m,s=Date.addHMS(dti,12);
"%s %d %d %s".fmt(Date.monthNames[M],D,Y,Date.toAMPMString(h,m));
}</
<
{{out}}
<pre>March 8 2009 07:30AM</pre>
|