Date Time Parsing And Formatting
TimeToStr/TimeToString only offer a couple of fixed layouts, and StringToTime only accepts "YYYY.MM.DD HH:MI" - anything a log file, a CSV export, a webhook payload or an external API actually uses needs its own parser, and most EAs end up with a one-off, half-tested version buried in a utility function. This header uses one small token vocabulary - YYYY, YY, MM, DD, hh, mm, ss - for both directions. DateTimeFormat expands the tokens; DateTimeParseCustom consumes the same tokens back out of a string built to the identical layout. That symmetry is the point: format a value with a layout string, parse it back with the same layout string, and you get the original instant back exactly, which the demo verifies directly rather than asserting it. Parsing fails loudly rather than guessing. StructToTime, the platform function this header eventually calls, silently normalises out-of-range fields - month 13 quietly becomes month 1 of the following year - which would let a malformed input parse into a wrong-but-plausible date with no error. DateTimeParseCustom range-checks every field itself first and reports failure through an explicit out-parameter, never a normalised guess. ISO 8601 helpers cover the read/write format almost every external API and log aggregator expects, accepting both the "T" and space separator styles on the way in. Calendar helpers (AddDays, DiffSeconds, StartOfDay, leap year, days in month) round out the everyday date arithmetic every EA eventually needs, backed by the platform's own weekday calculation rather than a hand-rolled Zeller's congruence. The token scanner, the parser and the calendar maths are the same algorithm on both platforms - datetime is a plain seconds-since-epoch integer with no time zone database on either MQL4 or MQL5, so nothing about the format tokens or the arithmetic changes between them. Notes and limitations - datetime is a plain seconds-since-epoch value with no time zone or DST database attached on either platform. AddDays/AddHours/etc. are exact second-count offsets, not calendar-aware - "add 1 day" is always +86400 seconds, which is correct in server time but is not the same operation a DST-aware calendar library would perform across a spring-forward or fall-back boundary elsewhere. - The MMM (three-letter month name) token is formatting-only. DateTimeParseCustom does not accept it, because parsing a month name back correctly needs a locale table this header deliberately does not carry. Use numeric MM for any layout you intend to parse back. - No time zone suffix ("Z", "+02:00") is written or read. Every value here is treated as being in whatever clock produced it - typically broker server time - and the caller is responsible for knowing which. - Year range is validated to 1970-2100 as a sanity bound, not a hard platform limit.
Script · MT5 · v1.0 · Free
About Date Time Parsing And Formatting
Date Time Parsing And Formatting is a script for MetaTrader 5. It has 1 setting, such as Custom format. MQL source code included.
Files in the download: DateTimeUtils.mq5.mqh, DateTimeUtils.txt, DateTimeUtilsDemo.ex5, DateTimeUtilsDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Custom format | DD/MM/YYYY hh:mm:ss | Layout to format and round-trip |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Risk & Lot Size · MT5 Script · MQL5 Source Code