Date and Time#
PowerLanguage has two date times: one is the date time of the bar, and the other is the date time of the current computer.
Date time has two formats: DateTime and ELDate/ELTime. The DateTime and ELDate/ELTime formats are not intuitive to read, so PowerLanguage provides format conversion functions to convert between DateTime, ELDate/ELTime, String, and Numerical.
DateTime#
DateTime is a floating-point number. The integer represents the date, indicating how many days have passed since 1900/1/1. The decimal represents the time, indicating the proportion of one day that has elapsed since 0:00.
ELDate/ELTime#
| Format | Description |
|---|
ELDate | Represents a date in YYYMMdd format. YYY: how many years have passed since 1900. MM: month. dd: day |
ELTime | Represents time in 24-hour HHmm or HHmmss format (e.g., 130000 = 1:00:00 PM), which one is used depends on the function called. HH: hours. mm: minutes. ss: seconds |
Date and Time Functions#
Date Time of the Bar#
| Function | Description |
|---|
Date | Returns the closing date of the bar in ELDate YYYMMdd format. Example: 2007/10/30 returns 1071030, 1999/04/02 returns 990402 |
Time | Returns the closing time of the bar in ELTime HHmm format. Example: 10:15 AM returns 1015, 3:45 PM returns 1545 |
Time_s | Returns the closing time of the bar in ELTime HHmmss format. Example: 10:15:25 AM returns 101525, 3:45:00 PM returns 154500 |
DateTime | Returns the closing date and time of the bar in DateTime format. Example: 2013/5/28 08:41:11.813 returns 41422.74 |
Date Time of the Current Computer#
| Function | Description |
|---|
CurrentDate | Returns the current date of the computer in ELDate YYYMMdd format. Example: 2008/10/30 returns 1081030 |
CurrentTime | Returns the current time of the computer in ELTime HHmm format. Example: 10:15 AM returns 1015, 3:45 PM returns 1545 |
CurrentTime_s | Returns the current time of the computer in ELTime HHmmss format. Example: 10:15:25 AM returns 101525, 3:45:00 PM returns 154500 |
ComputerDateTime | Returns the current date and time of the computer in DateTime format. Equivalent to ELDateToDateTime(CurrentDate) + ELTimeToDateTime(CurrentTime). Example: 2008/1/1 6:00 AM returns 39448.25000000 |
The following table lists the conversion functions between formats:
DateTime -> ELTime#
| Function | Description |
|---|
DateTime2ELTime(DateTime) | Converts the decimal (time part) of DateTime to ELTime HHmm and returns it. Example: DateTime2ELTime(39449.65625000) returns 1545, which means 3:45 PM |
DateTime2ELTime_s(DateTime) | Converts the decimal (time part) of DateTime to ELTime HHmmss and returns it. Example: DateTime2ELTime_s(39449.646354167) returns 153045, which means 3:30:45 PM |
ELDate/ELTime -> DateTime#
| Function | Description |
|---|
ELDateToDateTime(YYYMMdd) | Converts ELDate YYYMMdd to the integer (date part) of DateTime and returns it. Example: ELDateToDateTime(1080101) returns 39448.00000000 (2008/1/1) |
ELTimeToDateTime(HHmm) | Converts ELTime HHmm to the decimal (time part) of DateTime and returns it. Example: ELTimeToDateTime(1015) returns 0.42708333 (10:15 AM) |
ELTimeToDateTime_s(HHmmss) | Converts ELTime HHmmss to the decimal (time part) of DateTime and returns it. Example: ELTimeToDateTime_s(101525) returns 0.427372685 (10:15:25 AM) |
DateTime -> String#
| Function | Description |
|---|
DateTimeToString(DateTime) | Converts the integer (date part) and decimal (time part) of DateTime to a string and returns it. Example: DateTimeToString(ComputerDateTime) returns the current date and time string of the computer |
DateTimeToString_ms(DateTime) | Converts the integer (date part) and decimal (time part) of DateTime to a string with millisecond precision and returns it. Example: DateTimeToString_ms(DateTime) returns the date and time string of the bar |
DateToString(DateTime) | Converts the integer (date part) of DateTime to a string and returns it. Example: DateToString(39448.25000000) returns "1/1/2008" |
TimeToString(DateTime) | Converts the decimal (time part) of DateTime to a string and returns it. Example: TimeToString(39448.75000000) returns "6:00 PM" |
TimeToString_ms(DateTime) | Converts the decimal (time part) of DateTime to a string with millisecond precision and returns it. Example: TimeToString_ms(39448.7501) returns "18:00:08.640" |
FormatDate("FormatString", DateTime) | Converts the integer (date part) of DateTime to a formatted string and returns it. FormatString format characters: see Date Format Characters |
FormatTime("FormatString", DateTime) | Converts the decimal (time part) of DateTime to a formatted string and returns it. FormatString format characters: see Time Format Characters |
| Character | Description |
|---|
d | Day of the month, no leading zero for numbers less than 10 |
dd | Day of the month, with leading zero for numbers less than 10 |
ddd | Three-letter abbreviation for the day of the week |
dddd | Full name of the day of the week |
M | Month number, no leading zero for numbers less than 10 |
MM | Month number, with leading zero for numbers less than 10 |
MMM | Three-letter abbreviation of the month |
MMMM | Full name of the month |
y | Last two digits of the year, no leading zero for numbers less than 10 |
yy | Last two digits of the year, with leading zero for numbers less than 10 |
yyyy | Four-digit year |
Example:
1
2
3
4
5
6
7
8
9
10
11
| // Returns "Tuesday, January 22, 2008."
FormatDate("dddd, MMMM dd, yyyy.", 39469.250);
// Returns "1/22/8"
FormatDate("M/d/y", 39469.250);
// Returns "22-01-08"
FormatDate("dd-MM-yy", 39469.250);
// Returns "Next Tue is: Jan 22"
FormatDate("Next ddd is: MMM dd", 39469.250);
|
| Character | Description |
|---|
h | 12-hour hours, no leading zero for numbers less than 10 |
hh | 12-hour hours, with leading zero for numbers less than 10 |
H | 24-hour hours, no leading zero for numbers less than 10 |
HH | 24-hour hours, with leading zero for numbers less than 10 |
m | Minutes, no leading zero for numbers less than 10 |
mm | Minutes, with leading zero for numbers less than 10 |
s | Seconds, no leading zero for numbers less than 10 |
ss | Seconds, with leading zero for numbers less than 10 |
t | Single character of AM or PM |
tt | AM or PM |
Example:
1
2
3
4
5
6
7
8
9
10
11
| // Returns "04:01:03 P"
FormatTime("hh:mm:ss t", 39469.6674);
// Returns "4 PM"
FormatTime("h tt", 39469.6674);
// Returns "16:01"
FormatTime("HH:mm", 39469.6674);
// Returns "1 MIN 3 SEC"
FormatTime("m MIN s SEC", 39469.6674);
|
String -> DateTime#
| Function | Description |
|---|
StringToDate("MM/dd/yy") | Converts the string "MM/dd/yy" or "MM/dd/yyyy" to the integer (date part) of DateTime and returns it. MM: month. dd: day. yy/yyyy: year. Example: StringToDate("04/04/99") returns 36254.0000000 (1999/4/4) |
StringToTime("hh:mm:ss tt") | Converts the string "hh:mm:ss tt" to the decimal (time part) of DateTime and returns it. hh: 12-hour hours. mm: minutes. ss: seconds. tt: AM or PM. Example: StringToTime("08:00:00 AM") returns 0.33333333 |
StringToDateTime("MM/dd/yy hh:mm:ss tt") | Converts the string "MM/dd/yy hh:mm:ss tt" or "MM/dd/yyyy hh:mm:ss tt" to DateTime and returns it. Example: StringToDateTime("04/04/99 04:48:00 PM") returns 36254.70000000 (1999/4/4 4:48 PM) |
DateTime -> Numerical#
| Function | Description |
|---|
YearFromDateTime(DateTime) | Returns the year value of DateTime. Example: YearFromDateTime(39449.25000000) returns 2008 |
MonthFromDateTime(DateTime) | Returns the month value of DateTime. Example: MonthFromDateTime(39600.25000000) returns 6 (June) |
HoursFromDateTime(DateTime) | Returns the 24-hour hour value of DateTime. Example: HoursFromDateTime(39449.85000000) returns 20 (8 PM) |
MinutesFromDateTime(DateTime) | Returns the minutes value of DateTime. Example: MinutesFromDateTime(39449.35000000) returns 24 |
SecondsFromDateTime(DateTime) | Returns the seconds value of DateTime. Example: SecondsFromDateTime(39449.35440000) returns 20 |
MillisecondsFromDateTime(DateTime) | Returns the milliseconds value of DateTime |
Numerical -> DateTime#
| Function | Description |
|---|
EncodeDate(yy, MM, dd) | Converts numerical values yy (last two digits of year), MM (month), dd (day) to DateTime and returns it. Example: EncodeDate(08, 01, 01) returns 39448.00000000 (2008/1/1) |
EncodeTime(HH, mm, ss, mmm) | Converts numerical values HH (24-hour hours), mm (minutes), ss (seconds), mmm (milliseconds) to DateTime and returns it. Example: EncodeTime(16, 29, 55, 500) returns 0.6874479167 (16:29:55.500) |
Example#
Print the debug log (Open, High, Low, Close) to a file within the specified date range 04/08/2022~04/09/2022.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| Inputs: startDateOfLog("04/08/2022");
Inputs: endDateOfLog("04/09/2022");
Variables:
dateStr(""), timeStr(""),
kBarTimeStr(""), kBarDateStr(""),
formatStr(""), filePathStr("");
Variables: 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 if the date is within range
isEnableLogOfDate = StringToDate(startDateOfLog) <= DateTime
and DateTime <= StringToDate(endDateOfLog);
// Print debug log to file within specified date range
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