Date routines

Metro 4 contains function to format date and time and output formatted string and create date from string by mask.

Date to string

You can format date and time value to formatted string with special function. This function is implemented as an extension of the Date object with name format. This function has two parameters:

  • format - format for output.
  • locale - language for output.

You can read more about locales in this article.


                    Date.prototype.format = function(format, locale){...}

                    var date = new Date();
                    console.log(date.format("%d-%m-%Y"));
                
Patterns
Pattern Desc
%a The abbreviated name of the day of the week according to the locale.
%A The full name of the day of the week according to the locale.
%b The abbreviated month name according to the locale.
%B The full name of the month according to the locale.
%c Preferred date and time (UTC) representation for the locale.
%C Century number (year/100) as a 2-digit integer.
%d Day of the month as a decimal number (range 01 to 31).
%e Day of the month as a decimal number (range 1 to 31).
%F ISO 8601 date format (equivalent to %Y-%m-%d).
%G ISO 8601 week-based year with century as a decimal number. The 4-digit year corresponds to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead.
%g Like %G, but without century, that is, with a 2-digit year (00-99).
%H Hour as a decimal number using a 24-hour clock (range 00 to 23). See also %k.
%I Hour as a decimal number using a 12-hour clock (range 01 to 12). See also %l.
%j Day of the year as a decimal number (range 001 to 366).
%k Hour as a decimal number using a 24-hour clock (range 0 to 23). See also %H.
%l Hour as a decimal number using a 12-hour clock (range 1 to 12). See also %I.
%m Month as a decimal number (range 01 to 12).
%M Minute as a decimal number (range 00 to 59).
%p Either "AM" or "PM" according to the given time value. Noon is treated as "PM" and midnight as "AM".
%P Like %p but in lowercase ("am" or "pm").
%s Number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
%S Second as a decimal number (range 00 to 59).
%u Day of the week as a decimal (range 1 to 7), Monday being 1. See also %w.
%V ISO 8601 week number of the year as a decimal number (range 01 to 53), where week 1 is the first week that has at least 4 days in the new year (that is, the first Thursday).
%w Day of the week as a decimal (range 0 to 6), Sunday being 0. See also %u.
%x Preferred date representation for the locale without the time.
%X Preferred time representation for the locale without the date.
%y Year as a decimal number without a century (range 00 to 99).
%Y Year as a decimal number including the century.
%z The +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from UTC).
%Z Timezone name or abbreviation.

String to date

You can convert string to date by mask with special string extension function toDate(mask).


                    String.prototype.toDate = function(mask){...}

                    "1972-12-21 04:30:00".toDate("yyyy-mm-dd hh:ii:ss");
                    "21-12-1972".toDate("dd-mm-yyyy");
                    "21-12-1972".toDate("%d-%m-%y");
                
Patterns
Pattern Desc
%d, dd Day definition.
%m, mm Month definition.
%y, yy, yyyy Year definition.
%h, hh Hours definition.
%i, ii, mi Minutes definition.
%s, ss Seconds definition.