MultiCharts | PowerLanguage Tutorials | CH10: Date and Time

Date and Time

PowerLanguage has two date time, one is the date time of the bar, and the other is the date time of the current computer (the current date time).
The date time can be represented in two formats: DateTime and ELDate/ELTime.
However, the format of DateTime and ELDate/ELTime is not human-friendly to read, so PowerLanguage provides a serise of functions converting formats between DateTime, ELDate/ELTime, String and Numerical.

Format

The date time can be represented in two formats: DateTime and ELDate/ELTime.

DateTime
DateTime A floating-point number. The integer represents the date, indicating how many days have passed since January 1, 1900, and the decimal represents the time, indicating the proportion of one day that has elapsed since 0:00.
ELDate/ELTime
ELDate Represents a date in YYYMMdd format.
YYY – how many years have passed since 1900.
MM – month.
dd –  day of the month.
ELTime The time in 24-hour HHmm or HHmmss format which one is used depends on the function parameter.
HH – hours.
mm – minutes.
ss – seconds.

Functions

PowerLanguage has two date time, one is the date time of the bar, and the other is the date time of the current computer (the current date time).

Date time of the bar

Function  
Date Description Returns the closing date of the bar, which is represented in ELDate:YYYMMdd.
Example Print(Date);
Returns 1071030 on the date October 30th, 2007.
Returns 990402 on the date April 2th, 1999.
Time Description Returns the closing time of the bar, which is represented in ELTime:HHmm.
Example Time
Returns 1015 at time 10:15 AM.
Returns 1545 at time 3:45 PM.
Time_s Description Returns the closing time of the bar, which is represented in ELTime:HHmmss.
Example Time_s
Returns 101525 at time 10:15:25 AM.
Returns 154500 at time 3:45:00 PM.
DateTime Description Returns the closing date and time of the bar, which is represented in DateTime.
Example DateTime
Returns 41422.74 at 5/28/2013 08:41:11.813.

Date time of the current computer

Function  
CurrentDate Description Returns the current date of the computer, which is represented in ELDate:YYYMMdd.
Example Print(CurrentDate);
Returns 1081030 on the date October 30th, 2008.
CurrentTime Description Returns the current time of the computer, which is represented in ELTime:HHmm.
Example CurrentTime
Returns 1015 at time 10:15 AM.
Returns 1545 at time 3:45 PM.
CurrentTime_s Description Returns the current time of the computer, which is represented in ELTime:HHmmss.
Example CurrentTime_s
Returns 101525 at time 10:15:25 AM.
Returns 154500 at time 3:45:00 PM.
ComputerDateTime Description Returns the current date and time of the computer, which is represented in DateTime.
ComputerDateTime = 
ELTimeToDateTime(CurrentTime) + ELDateToDateTime(CurrentDate)
Example ComputerDateTime
Returns 39448.25000000 at 6:00 AM on January 1st, 2008.

Format conversion

The format of DateTime and ELDate/ELTime is not human-friendly to read, so PowerLanguage provides a serise of functions converting formats between DateTime, ELDate/ELTime, String and Numerical.

  Destination
DateTime ELDate/ELTime String Numerical
Source DateTime NA DateTime -> ELTime DateTime -> String DateTime -> Numerical
ELDate/ELTime ELDate/ELTime -> DateTime NA NA NA
String String -> DateTime NA NA NA
Numerical Numerical -> DateTime NA NA NA

DateTime -> ELTime

DateTime2ELTime(DateTime) Description Convert the decimal (time part) of DateTime to ELTime:HHmm and return it.
Example DateTime2ELTime(39449.65625000)
Returns 1545, which means 3:45 PM.
DateTime2ELTime_s (DateTime) Description Convert the decimal (time part) of DateTime to ELTime:HHmmss and return it.
Example DateTime2ELTime_s(39449.646354167)
Returns 153045, which means 3:30:45 PM.
Jump to Format conversion

ELDate/ELTime -> DateTime

ELDateToDateTime(YYYMMdd) Description Convert ELDate:YYYMMdd to an integer (date part) of DateTime and return it.
Example ELDateToDateTime(1080101)
Convert January 1st, 2008 to 39448.00000000.
ELDateToDateTime(990402)
Convert April 2nd, 1999 to 36252.00000000.
ELTimeToDateTime(HHmm) Description Convert ELTime:HHmm to a decimal (time part) of DateTime and return it.
Example ELTimeToDateTime(1015)
Convert 10:15 AM to 0.42708333.
ELTimeToDateTime(1545)
Convert 3:45 PM to 0.65625000.
ELTimeToDateTime_s(HHmmss) Description Convert ELTime:HHmmss to the decimal (time part) of DateTime and return it.
Example ELTimeToDateTime_s(101525)
Convert 10:15:25 AM to 0.427372685.
ELTimeToDateTime_s(154500)
Convert 3:45:00 PM to 0.656250000.
Jump to Format conversion

DateTime -> String

DateTimeToString(DateTime) Description Convert the integer (date part) and decimal (time part) of DateTime into strings and return it.
Example DateTimeToString(ComputerDateTime)
Returns the string of the current date and time of the computer.
DateTimeToString_ms(DateTime) Description Converts the integer (date part) and decimal (time part) of a DateTime into a string with millisecond precision and returns it.
Example DateTimeToString_ms(DateTime)
Returns the string of the closing date and time of the bar.
DateToString(DateTime) Description Convert the integer (date part) of DateTime into a string and return it.
Example DateToString(39448.25000000)
Returns the string “1/1/2008”.
TimeToString(DateTime) Description Convert the decimal (time part) of DateTime into a string and return it.
Example TimeToString(39448.75000000)
Returns the string “6:00 PM”.
TimeToString_ms(DateTime) Description Convert the decimal (time part) of DateTime to a string with millisecond precision and return it.
Example TimeToString_ms(39448.7501)
Return string “18:00:08.640”.
FormatDate("FormatString", DateTime) Description Convert the integer (date part) of DateTime into a format string (“FormatString“) and return it.
FormatString – a format string, specifying the output format of the string, using the following characters to form the format string:
d Day of the month, no 0 is added in front of numbers less than 10.
dd Day of the month, with 0 added in front of numbers less than 10.
ddd Three-letter abbreviation for the day of the week.
dddd The full name of the day of the week.
M Number of the month, no 0 is added in front of the number less than 10.
MM Number of the month, with 0 added in front of numbers less than 10.
MMM Three-letter abbreviation of the month.
MMMM The full name of the month.
y The last two digits of the year, with zeros in front of the digits less than 10.
yy The last two digits of the year, with 0 added in front of numbers less than 10.
yyyy Four digits of the year.
Examples FormatDate("dddd, MMMM dd, yyyy.",39469.250)
Returns the string “Tuesday, January 22, 2008.”
FormatDate("M/d/y",39469.250)
Returns the string “1/22/8”
FormatDate("dd-MM-yy",39469.250)
Returns the string “22-01-08”
FormatDate("Next ddd is: MMM dd",39469.250)
Returns the string “Next Tue is: Jan 22”
FormatTime("FormatString", DateTime) Description Convert the decimal (time part) of DateTime into a format string (“FormatString“) and return it
FormatString – a format string, specifying the output format of the string, using the following characters to form the format string:
h Hours in 12-HR AM/PM format, no 0 is added in front of the number less than 10.
hh Hours in 12-HR AM/PM format, with 0 added in front of numbers less than 10.
H Hours in 24-HR format, no 0 is added in front of numbers less than 10.
HH Hours in 24-HR format, with 0 added in front of numbers less than 10.
m Minutes, no 0 is added in front of numbers less than 10.
mm Minutes, with 0 added in front of numbers less than 10.
s Seconds, no 0 is added in front of numbers less than 10.
ss Seconds, numbers less than 10 are preceded by 0s.
t A single character of AM or PM.
tt AM or PM.
Example FormatTime("hh:mm:ss t",39469.6674)
Returns the string “04:01:03 P”
FormatTime("h tt",39469.6674)
Returns the string “4 PM”
FormatTime("HH:mm",39469.6674)
Returns the string “16:01”
FormatTime("m MIN s SEC",39469.6674)
Return string “1 MIN 3 SEC”
Jump to Format conversion

String -> DateTime

StringToDate("MM/dd/yy")
or
StringToDate("MM/dd/yyyy")
Description Convert the string “MM/dd/yy” or “MM/dd/yyyy” to an integer (date part) of DateTime and return it.
Where:
MM – month.
dd – the day of the month.
yy – the last two digits of the year.
yyyy – Four digits of the year.
Example StringToDate("04/04/99")
Convert April 4th, 1999 to 36254.0000000.
StringToTime("hh:mm:ss tt") Description Convert the string “hh:mm:ss tt” to the decimal (time part) of DateTime and return it.
Where:
hh – hours, in 12-HR AM/PM format.
mm – minutes.
ss – seconds.
tt – AM or PM.
Example StringToTime ("08:00:00 AM")
Convert 08:00:00 AM to 0.33333333.
StringToDateTime("MM/dd/yy hh:mm:ss tt")
or
StringToDateTime("MM/dd/yyyy hh:mm:ss tt")
Description Convert the string “MM/dd/yy” or “MM/dd/yyyy” to DateTime integer (date part) and decimal (time part) and return it.
Where:
MM – month.
dd – the day of the month.
yy – the last two digits of the year.
yyyy – four digits of the year.
hh – hours in 12-hour AM/PM format.
mm – minutes.
ss – seconds.
tt – AM or PM.
Example StringToDateTime("04/04/99 04:48:00 PM")
Convert 04:48:00 PM on April 4th, 1999 to 36254.70000000.
Jump to Format conversion

DateTime -> Numerical

YearFromDateTime(DateTime) Description Returns the number of the year of the DateTime
Example YearFromDateTime(39449.25000000)
Returns the number 2008, indicating the year 2008.
MonthFromDateTime(DateTime) Description 回傳DateTime的月份數字。
Returns the number of the month of the DateTime.
Example MonthFromDateTime(39600.25000000)
Return the number 6, which means June.
HoursFromDateTime(DateTime) Description Returns the number of 24-HR hour of the DateTime.
Example HoursFromDateTime(39449.85000000)
Returns the number 20, which means 8 PM.
MinutesFromDateTime(DateTime) Description Returns the number of minutes of the DateTime.
Example MinutesFromDateTime(39449.35000000)
Returns the number 24, which means 24 minutes.
SecondsFromDateTime(DateTime) Description Returns the number of seconds of the DateTime.
Example SecondsFromDateTime (39449.35440000)
Returns the number 20, which means 20 seconds.
MillisecondsFromDateTime(DateTime);
Description Returns the number of milliseconds of the DateTime.
Example MillisecondsFromDateTime(DateTime)
Returns the milliseconds of the current bar.
Jump to Format conversion

Numerical -> DateTime

EncodeDate(yy, MM, dd) Description Convert the numbers yy, MM, and dd to DateTime and return it.
yy – the last two digits of the year.
MM – the month number.
dd – the date number.
Example EncodeDate(08, 01, 01)
Convert January 1st, 2008 to 39448.00000000.
EncodeTime(HH, mm, ss, mmm) Description Convert the numbers HH, mm, ss, mmm to DateTime and return it.
HH – hours, in 24-hour format.
mm – minutes.
ss – seconds .
mmm – milliseconds.
Example EncodeTime(16, 29, 55, 500)
Convert 16:29:55.500 to 0.6874479167.
Jump to Format conversion

Example

The following example is print the debug log (open, high, low, close) within the specified date: 04/08/2022-04/09/2022 to the file.

inputs: startDateOfLog("04/08/2022");
inputs: endDateOfLog("04/09/2022");

var: dateStr(""), timeStr(""), kBarTimeStr(""), kBarDateStr(""), formatStr(""), filePathStr("");
var: isEnableLogOfDate(false);

//* date and time formatting */
dateStr=FormatDate("yyyy-MM-dd", ComputerDateTime);
timeStr=FormatTime("HH:mm:ss", ComputerDateTime);
kBarTimeStr=FormatTime("HH:mm:ss", DateTime);
kBarDateStr=FormatDate("yyyy-MM-dd", DateTime);
formatStr="Computer Date Time: "+dateStr+" "+timeStr+", K-bar Date Time: "+kBarDateStr+" "+kBarTimeStr;


//file path
filePathStr="D:\MCLog\"+getsymbolname+"_"+getstrategyname+".log";

//check the date
isEnableLogOfDate=StringToDate(startDateOfLog)<=DateTime and DateTime<=StringToDate(endDateOfLog);

//print debug log to file within specified date
if isEnableLogOfDate then begin
	print(File(filePathStr), formatStr, "** Start Log **");
	print(File(filePathStr), ", open: ", open, ", high: ", high, ", low: ", low, ", close: ", close);
end;

Reference

https://www.multicharts.com/trading-software/index.php?title=Category:Date_and_Time_Routines

https://www.multicharts.com/trading-software/index.php?title=Category:Data_Information/General

https://www.multicharts.com/trading-software/index.php?title=Outputting_Dates_in_EasyLanguage

https://www.multicharts.com/trading-software/index.php?title=DateTimeToString_Ms

Leave a Reply