Converting a true BMT ISO date time string, such as a javascript generated UTC date time string in 4D requires a litte bit more than parsing a 4D generated UTC date time string
The javascript function below will return a ISO date time string for GMT relative the the current time zone on the users machine.function getUtcDateTimeString() {
var now = new Date();
var nowUTC = now.toISOString();
return nowUTC;
}
The javascript generated ISO GMT date time string will contain the actual GMT date and time offset from the system current time zone, including decimal seconds, e.g. "2014-07-21T15:56:18.353Z".
The utility provide beow will convert a true GMT ISO date time string into a properly formated ISO local date time string.If (True)
If (False)
Begin SQL
/*
Name: UTIL_DateTime_GMT_To_Local (GMTdateTimeString) -> C_OBJECT
Path: UTIL_DateTime_GMT_To_Local
Purpose: Convert ISO GMT date string to local time, date and ISO date string
$0 - OBJECT - Local date, time and string
$1 - TEXT - GMT Date< Time String
*/
End SQL
End if
C_TEXT($MethodName_T)
$MethodName_T:=Current method name
//===================== Declare Variables ==================================
//method_parameters_declarations
C_OBJECT($0;$LocalDtTi_O)
C_TEXT($DTStr_GMT_T;$1)
//--------------------------------------------------------------------------
//method_wide_constants_declarations
//--------------------------------------------------------------------------
//local_variable_declarations
C_LONGINT($Ndx;$SOA;$RIS;$Params_L)
C_DATE($DtGMT_D;$DtLocal_D)
C_TIME($TiGMT_H;$TiLocal_H)
C_LONGINT($Offset_L)
C_TEXT($GMToffset_T;$DTStr_GMT_T;$DTStr_T)
End if
//====================== Initialize and Setup ================================
$Params_L:=Count parameters
If (Asserted($Params_L>=1;"Expected on paramter"))
$DTStr_GMT_T:=$1
$GMToffset_T:=UTIL_GetTimezoneOffset // see tech tip https://kb.4d.com/assetid=76220
//======================== Method Actions ==================================
// Extract the hours difference and convert to seconds
//
$Offset_L:=Num(Substring($GMToffset_T;1;Position(":";$GMToffset_T)-1))*(60*60)
// If $DTStr_GMT_T is a properly formated with "Z" time zone
// 4D automatically applies date diff if any, otherwise date is unchanged
//
$DtGMT_D:=Date($DTStr_GMT_T)
$TiGMT_H:=Time(Substring($DTStr_GMT_T;12))
Case of
: ($Offset_L<0) // Time zones west of GMT
$TiLocal_H:=Time($TiGMT_H+$Offset_L)
$DtLocal_D:=$DtGMT_D
If ($TiLocal_H<0)
$TiLocal_H:=$TiLocal_H+?12:00:00?
End if
: ($Offset_L>0) // Time zones east of GMT
$TiLocal_H:=Time($TiGMT_H+$Offset_L)
$DtLocal_D:=$DtGMT_D
If ($TiLocal_H>(?24:00:00?))
$TiLocal_H:=$TiLocal_H-?24:00:00?
End if
Else // Same GMT zone
$TiLocal_H:=$TiGMT_H
$DtLocal_D:=$DtGMT_D
End case
// Set the local to AM or PM
//
$DTStr_T:=Substring(String($DtLocal_D;ISO date);1;11)+String($TiLocal_H;HH MM SS)+Choose($TiLocal_H<?12:00:00?;" AM";" PM")
OB SET($LocalDtTi_O;"date";String($DtLocal_D))
OB SET($LocalDtTi_O;"time";String($TiLocal_H;HH MM SS))
OB SET($LocalDtTi_O;"string";$DTStr_T)
//======================== Clean up and Exit =================================
$0:=$LocalDtTi_O
End if
Below is example code for calling the conversion utility. Following the code example are two screenshots of conversions. The first is with the System time zone set to US East Coast (DST). The second is with the System time zone set the Australia East Coast (DST) // Local time in this example is US Eastern Daylight Savings Time
// Offset in this example is "-04:00:00", or -4 hours
//
// Example GMT Date time string generated via javascript
//
//$DTStr_GMT_T:="2014-07-22T02:28:25.625Z"//( US Eastcoast DST )
$DTStr_GMT_T:="2014-07-22T16:13:57.232Z" // ( Australia Eastcoast DST )
C_OBJECT($Resp_O)
$Resp_O:=UTIL_DateTim_GMT_To_Local ($DTStr_GMT_T)
$DtLocal_D:=Date(OB Get($Resp_O;"date"))
$TiLocal_H:=Time(OB Get($Resp_O;"time"))
$DTStr_T:=OB Get($Resp_O;"string")
Notice the highlighted dates. If you examine the utility you will notice that the date is never manipulate, only the time is adjusted to account for the GMT time zone offset. 4D automatically applied any adjustment needed to the date. It is automatic when the date time string ends with a "Z."
Screenshot #1 (US East coast)
Screenshot #2 (Australia East coast)