XScript Manual · Chapter 33

DATE Object — Date and Time

DATE is a collection of date and time functions. Call them directly on the object name, as in DATE.GetNow().

Times are passed as .NET Ticks values (long). One tick is 100 nanoseconds (1 second = 10,000,000 ticks), counted from 0001-01-01 00:00, so two times can be compared directly with < and >. Use ConvertDateTimeString to turn a value into a human-readable string.

Example

Current time and strings

xscript
long now = DATE.GetNow();                                          // current time ( PC local time )
string day  = DATE.ConvertDateString(now);                         // "2026-05-06"
string text = DATE.ConvertDateTimeString(now);                     // "2026-05-06 09:30:00"
string file = DATE.ConvertDateTimeString(now, "yyyyMMdd_HHmmss");  // "20260506_093000"
Log("now = {0}", text);

String to time — check that it can be read first

xscript
long StartTicks = 0;   // job start time ( ticks )
 
FUNCTION bool SetStartTime(string text)
{
   long t = 0;
   if( DATE.TryParseDateTime(text, /*ref*/t) == false )   // false if it cannot be read, t becomes 0
   {
      LogError("invalid date : {0}", text);
      return false;
   }
   StartTicks = t;
   return true;
}

Showing elapsed time

xscript
long WorkStart = 0;   // job start time ( ticks )
 
FUNCTION void OnWorkStart()
{
   WorkStart = DATE.GetNow();
}
 
FUNCTION string GetElapsedText()
{
   long sec = DATE.SecondsBetween(DATE.GetNow(), WorkStart);   // difference between two times ( seconds, truncated )
   return $"{sec / 60}:{sec % 60:D2}";                          // 725 seconds → "12:05"
}

Waiting until a deadline

xscript
FUNCTION bool WaitJobDone()
{
   long deadline = DATE.IncrementSeconds(DATE.GetNow(), 30);   // 30 seconds from now
   while( DATE.GetNow() < deadline )
   {
      if( IO["I_JobDone"] == true )
      {
         return true;
      }
      Sleep(50);
   }
   LogError("job done timeout");
   return false;
}

Per-day folder · splitting into year, month, day

xscript
string folder = "D:\\Logs\\" + DATE.ConvertDateString(DATE.GetNow());   // D:\Logs\2026-05-06
SYS.CreateDirectory(folder);                                              // true even if it already exists
 
int year = 0;
int month = 0;
int date = 0;
DATE.DecodeDate(DATE.GetNow(), /*ref*/year, /*ref*/month, /*ref*/date);   // the variables receive year, month, day
Log("today = {0}-{1}-{2}", year, month, date);

Methods

Pass a variable to a parameter marked &. When the call returns, the result is stored in that variable; passing a value or an expression is a build error (a /*ref*/ comment at the call site is recommended — see Functions).

Current time

MethodDescription
long GetNow()Returns the current time (PC local time) in ticks.
long GetUtcNow()Returns the current time (UTC) in ticks.

Ticks ↔ string

MethodDescription
string ConvertDateString(long date)Returns a yyyy-MM-dd string (e.g. 2026-05-06).
string ConvertDateTimeString(long date)Returns a yyyy-MM-dd HH:mm:ss string (e.g. 2026-05-06 09:30:00).
string ConvertDateTimeString(long date, string format)Returns a string built with format (e.g. "yyyyMMdd_HHmmss" → 20260506_093000). See the Notes section below for format characters.
long ConvertDateTimeLong(string date)Converts a date/time string to ticks. If the string has a time zone such as Z or +09:00, the result is converted to PC local time. A string that cannot be read raises a runtime error.
long ConvertUtcDateTimeLong(string date)Converts a date/time string to UTC ticks. Without a time zone, the string is taken as PC local time and converted to UTC (on a PC set to Korea time, "2026-05-06 09:30:00" → 00:30 UTC). With Z, the time is used as is. A string that cannot be read raises a runtime error.
bool TryParseDateTime(string dateStr, long &dateTicks)Returns true and stores the ticks in dateTicks if the string can be read. Otherwise returns false and sets the variable to 0. Never raises a runtime error.

Local ↔ UTC

MethodDescription
long ConvertLocalToUtc(long localTicks)Converts local-time ticks to UTC ticks (using the PC time zone).
long ConvertUtcToLocal(long utcTicks)Converts UTC ticks to local-time ticks.

Build · split

MethodDescription
long EncodeDate(int AYear, int AMonth, int ADay)Builds the ticks for 00:00 of the given date. A date that does not exist (e.g. February 30) raises a runtime error.
long EncodeDateTime(int AYear, int AMonth, int ADay, int AHour, int AMinute, int ASecond, int AMilliSecond)Builds ticks from year, month, day, hour, minute, second and millisecond. Out-of-range values raise a runtime error.
void DecodeDate(long date, int &AYear, int &AMonth, int &ADay)Splits ticks into year, month and day and stores them in the variables.
void DecodeDateTime(long date, int &AYear, int &AMonth, int &ADay, int &AHour, int &AMinute, int &ASecond, int &AMilliSecond)Splits ticks into year, month, day, hour, minute, second and millisecond and stores them in the variables.

Difference between two times

All of these return the difference as a positive number. Swapping the arguments gives the same result, and the remainder is truncated.

MethodDescription
int YearsBetween(long dateNow, long dateThen)Number of full years. Between 2025-05-07 and 2026-05-06 it is 0.
int MonthsBetween(long dateNow, long dateThen)Number of full months.
int WeeksBetween(long dateNow, long dateThen)Number of weeks (7-day units).
int DaysBetween(long dateNow, long dateThen)Number of days (24-hour units).
long HoursBetween(long dateNow, long dateThen)Number of hours.
long MinutesBetween(long dateNow, long dateThen)Number of minutes.
long SecondsBetween(long dateNow, long dateThen)Number of seconds.
long MilliSecondsBetween(long dateNow, long dateThen)Number of milliseconds.

Adding · subtracting time

All of these return the result in ticks. A negative amount moves back in time.

MethodDescription
long IncrementYears(long datetime, int years)Adds years. A day that does not exist in the target year becomes the last day of that month (2024-02-29 + 1 year → 2025-02-28).
long IncrementMonths(long datetime, int months)Adds months. A day that does not exist in the target month becomes the last day of that month (2026-01-31 + 1 month → 2026-02-28).
long IncrementDays(long datetime, int days)Adds days.
long IncrementHours(long datetime, int hours)Adds hours.
long IncrementMinutes(long datetime, int minutes)Adds minutes.
long IncrementSeconds(long datetime, int seconds)Adds seconds.
long IncrementMilliseconds(long datetime, int milliseconds)Adds milliseconds.

Notes

  • Ticks carry no time zone. GetNow() returns local time and GetUtcNow() returns UTC; the script must keep track of which is which. ConvertDateTimeString prints the value as it is.
  • Format characters — .NET date format strings are used: yyyy year · MM month · dd day · HH hour (24-hour) · mm minute · ss second · fff millisecond. They are case-sensitive (MM month / mm minute). Day and month names (ddd, MMM, etc.) appear in the language of the PC's regional settings.
  • Reading strings — ConvertDateTimeLong and TryParseDateTime read strings according to the PC's regional settings. Prefer the yyyy-MM-dd HH:mm:ss format over formats such as 05/06/2026, whose month/day order differs by region. A string produced by ConvertDateTimeString can be read back as is.
  • Functions that raise a runtime error — ConvertDateTimeLong and ConvertUtcDateTimeLong (string that cannot be read), EncodeDate and EncodeDateTime (non-existent date or out-of-range value). A runtime error stops execution at that point. Check strings that come from outside with TryParseDateTime first.
  • Function names are case-sensitive — note MilliSecondsBetween (capital S) versus IncrementMilliseconds (lowercase s). A call to an unknown name or with the wrong number of arguments is reported as a build error (Compile diagnostics).
  • There is no function that adds weeks. Use DATE.IncrementDays(t, 7 * n).
Subscribe