Global Functions
System Functions - Done
format date function formatdate(date, pattern, isinpututc, outputtz) description the formatdate function is designed for developers integrating it into various applications to handle date and time formatting across different timezones seamlessly automatic timezone detection by default, the function detects the user's (client's) timezone using dayjs tz guess() this makes it ideal for client side applications where the user's timezone can be accurately determined client side optimization since the function automatically detects the local timezone, it is primarily intended to be run client side this ensures that the formatted dates align with the user's local settings server side usage when running the function server side, dayjs tz guess() may detect the server's timezone instead of the client's to maintain consistency and avoid discrepancies, it is recommended to always set isinpututc to true when using the function in a server side environment this treats the input date as utc, ensuring that the output remains consistent regardless of the server's timezone timezone handling input timezone developers can specify if the input date is in utc by setting the isinpututc parameter to true if not specified, the function uses the detected local timezone output timezones developers can specify one or more additional output timezones regardless of the specified timezones, utc is always included in the output flexible formatting utilizes dayjs formatting tokens https //day js org/docs/en/display/format to allow customizable date and time representations validation validates the input date object and the format pattern to ensure accurate formatting handles invalid timezone inputs gracefully by skipping them and logging warnings, while still including utc in the output consistent output structure returns an object containing isvalid a boolean indicating the validity of the input date formatteddates an object mapping each specified timezone (including utc) to its corresponding formatted date string notes assumed local timezone for the examples provided, it's assumed that the user's local timezone is america/new york (utc 5) if your local timezone differs, adjust the expected outputs accordingly handling invalid timezones if an invalid timezone is provided in outputtz , the function will skip it and log a warning to the console utc will still be included in the output default behavior if outputtz is not specified, the function defaults to including both 'local' and 'utc' formatted dates including utc always the formatdate function is designed to always include utc in the output, ensuring a consistent reference point regardless of other specified timezones pattern flexibility ensure that the pattern provided is compatible with dayjs formatting tokens to achieve the desired date and time representations usage scenarios default formatting format a date using the local timezone and utc specify input as utc indicate that the input date is in utc and format accordingly, especially useful in server side applications multiple output timezones obtain formatted dates in multiple timezones alongside utc handle invalid inputs safely handle invalid dates or timezones without disrupting the output structure prerequisites install the dayjs library using npm npm install dayjs npm install dayjs plugin utc npm install dayjs plugin timezone parameters parameter type description date date the date object to format pattern string the format pattern following dayjs formatting tokens https //day js org/docs/en/display/format isinpututc boolean (optional) if set to true , the input date is treated as utc defaults to false (uses local timezone) outputtz string or string\[] (optional) specifies additional output timezones utc is always included in the output date pattern tokens the pattern string in the formatdate function uses dayjs formatting tokens to define the output format below is a table of common tokens you can use token description example output yyyy 4 digit year 2025 yy 2 digit year 25 mmmm full name of the month january mmm abbreviated name of the month jan mm 2 digit month 01 m month without leading zero 1 dd 2 digit day of the month 07 d day of the month without leading zero 7 dddd full name of the day of the week tuesday ddd abbreviated name of the day of the week tue hh 2 digit hour (24 hour clock) 14 h hour without leading zero (24 hour clock) 14 hh 2 digit hour (12 hour clock) 02 h hour without leading zero (12 hour clock) 2 mm 2 digit minutes 35 m minutes without leading zero 5 ss 2 digit seconds 09 s seconds without leading zero 9 a am/pm marker (uppercase) pm a am/pm marker (lowercase) pm z utc offset (e g , +05 00 ) +05 00 zz utc offset without colon (e g , +0500 ) +0500 for a complete list of tokens, refer to the dayjs documentation https //day js org/docs/en/display/format timezone patterns the inputtz and outputtz parameters accept iana timezone https //en wikipedia org/wiki/list of tz database time zones identifiers below is a table of common timezone options you can use for a complete list, refer to the iana time zone database https //www iana org/time zones timezone identifier location description utc coordinated universal time the primary time standard by which the world regulates clocks and time america/new york eastern time (us & canada) observes eastern standard time (est) and eastern daylight time (edt) america/chicago central time (us & canada) observes central standard time (cst) and central daylight time (cdt) america/denver mountain time (us & canada) observes mountain standard time (mst) and mountain daylight time (mdt) america/los angeles pacific time (us & canada) observes pacific standard time (pst) and pacific daylight time (pdt) europe/london london, united kingdom observes greenwich mean time (gmt) and british summer time (bst) europe/berlin berlin, germany observes central european time (cet) and central european summer time (cest) europe/moscow moscow, russia observes moscow standard time (msk) asia/tokyo tokyo, japan observes japan standard time (jst) asia/shanghai shanghai, china observes china standard time (cst) asia/kolkata kolkata, india observes india standard time (ist) australia/sydney sydney, australia observes australian eastern standard time (aest) and australian eastern daylight time (aedt) pacific/auckland auckland, new zealand observes new zealand standard time (nzst) and new zealand daylight time (nzdt) america/sao paulo são paulo, brazil observes brasília time (brt) and brasília summer time (brst) america/argentina/buenos aires buenos aires, argentina observes argentina time (art) africa/johannesburg johannesburg, south africa observes south africa standard time (sast) asia/dubai dubai, united arab emirates observes gulf standard time (gst) asia/singapore singapore observes singapore standard time (sgt) asia/bangkok bangkok, thailand observes indochina time (ict) asia/hong kong hong kong observes hong kong time (hkt) america/denver denver, usa observes mountain time (mt) america/vancouver vancouver, canada observes pacific time (pt) timezone notes case sensitivity timezone identifiers are case sensitive ensure that you use the correct casing (e g , america/new york not america/new york) daylight saving time (dst) many timezones observe dst the dayjs library automatically adjusts for dst based on the provided timezone invalid timezones if an invalid timezone identifier is provided, the function will return that the date is invalid ensure that the timezone strings used are valid iana identifiers return value the function returns an object containing the validity of the date and the formatted date string property type description isvalid boolean true if the date is valid and formatted successfully, false otherwise formatteddate string the formatted date string based on the provided pattern if isvalid is false, this will be an empty string notes assumed local timezone for these examples, it's assumed that the user's local timezone is america/new york (utc 5) if your local timezone differs, adjust the expected outputs accordingly function parameters explained date ( date ) the date to be formatted pattern ( string ) the format pattern following dayjs formatting tokens https //day js org/docs/en/display/format isinpututc ( boolean , optional) if set to true , the input date is treated as utc defaults to false (uses the user's local timezone) outputtz ( string or string\[] , optional) specifies additional output timezones utc is always included in the output handling invalid timezones if an invalid timezone is provided in outputtz , the function will skip it and log a warning to the console utc will still be included in the output default behavior if outputtz is not specified, the function defaults to including both 'local' and 'utc' formatted dates including utc always the formatdate function is designed to always include utc in the output, ensuring a consistent reference point regardless of other specified timezones pattern flexibility ensure that the pattern provided is compatible with dayjs formatting tokens to achieve the desired date and time representations example usage description input output valid date in yyyy mm dd format formatdate(new date('2025 01 07t14 35 12z'), 'yyyy mm dd') { "isvalid" true, "formatteddates" { "local" "2025 01 07", "utc" "2025 01 07" } } full day and month names with time formatdate(new date('2025 01 07t14 35 12z'), 'dddd, mmmm d, yyyy hh\ mm a') { "isvalid" true, "formatteddates" { "local" "tuesday, january 7, 2025 09 35 am", "utc" "tuesday, january 7, 2025 02 35 pm" } } date in abbreviated day and month format formatdate(new date('2025 01 07t14 35 12z'), 'ddd, mmm d, yy') { "isvalid" true, "formatteddates" { "local" "tue, jan 7, 25", "utc" "tue, jan 7, 25" } } time in 12 hour clock with minutes formatdate(new date('2025 01 07t14 35 12z'), 'hh\ mm a') { "isvalid" true, "formatteddates" { "local" "09 35 am", "utc" "02 35 pm" } } invalid date object formatdate(new date('invalid date'), 'yyyy mm dd') { "isvalid" false, "formatteddates" {} } specifying a single output timezone formatdate(new date('2025 01 07t14 35 12z'), 'yyyy mm dd hh a', true, 'america/new york') { "isvalid" true, "formatteddates" { "utc" "2025 01 07 02 pm", "america/new york" "2025 01 07 09 am" } } specifying multiple output timezones formatdate(new date('2025 01 07t14 35 12z'), 'yyyy mm dd hh a', true, \['america/new york', 'asia/tokyo']) { "isvalid" true, "formatteddates" { "utc" "2025 01 07 02 pm", "america/new york" "2025 01 07 09 am", "asia/tokyo" "2025 01 07 11 pm" } } combining default and specified timezones formatdate(new date('2025 01 07t14 35 12z'), 'yyyy mm dd hh a', true, \['local', 'asia/tokyo']) { "isvalid" true, "formatteddates" { "utc" "2025 01 07 02 pm", "local" "2025 01 07 09 am", "asia/tokyo" "2025 01 07 11 pm" } } handling invalid output timezones formatdate(new date('2025 01 07t14 35 12z'), 'yyyy mm dd hh a', true, \['invalid/timezone', 'asia/tokyo']) { "isvalid" true, "formatteddates" { "utc" "2025 01 07 02 pm", "asia/tokyo" "2025 01 07 11 pm" } } non string pattern formatdate(new date(), 12345) { "isvalid" false, "formatteddates" {} } default behavior (local and utc) formatdate(new date('2025 01 07t14 35 12z'), 'yyyy mm dd hh\ mm a') { "isvalid" true, "formatteddates" { "local" "2025 01 07 09 35 am", "utc" "2025 01 07 02 35 pm" } } specifying output timezone only formatdate(new date('2025 01 07t14 35 12z'), 'dddd, mmmm d, yyyy h\ mm a', false, 'europe/london') { "isvalid" true, "formatteddates" { "utc" "tuesday, january 7, 2025 02 35 pm", "europe/london" "tuesday, january 7, 2025 02 35 pm" } } multiple output timezones including default formatdate(new date('2025 01 07t14 35 12z'), 'yyyy mm dd hh\ mm a', true, \['local', 'utc', 'asia/kolkata']) { "isvalid" true, "formatteddates" { "utc" "2025 01 07 02 35 pm", "local" "2025 01 07 09 35 am", "asia/kolkata" "2025 01 07 08 05 pm" } } script const dayjs = require('dayjs'); const utc = require('dayjs/plugin/utc'); const timezone = require('dayjs/plugin/timezone'); // extend dayjs with necessary plugins dayjs extend(utc); dayjs extend(timezone); / formats a given date object into specified patterns using dayjs @param {date} date the date object to format @param {string} pattern the pattern defining the output format @param {boolean} \[isinpututc=false] (optional) if true, treats the input date as utc otherwise, uses the user's local timezone @param {string|string\[]} \[outputtz] (optional) the timezone(s) for the output formatted date(s) can be a single timezone string or an array of timezone strings @returns {{ isvalid boolean, formatteddates { \[timezone string] string } }} an object containing the validity of the date and an object of formatted date strings keyed by timezone @example formatdate(new date(), 'yyyy mm dd hh\ mm'); // { // isvalid true, // formatteddates { // 'local' "2025 01 07 09 35 am", // 'utc' "2025 01 07 14 35 pm" // } // } / function formatdate(date, pattern, isinpututc = false, outputtz) { // initialize return object const result = { isvalid false, formatteddates {} }; // validate the date object if (!(date instanceof date) || isnan(date)) { return result; // isvalid remains false, formatteddates is empty } // validate the pattern if (typeof pattern !== 'string') { return result; // isvalid remains false, formatteddates is empty } // determine the input timezone const inputtz = isinpututc ? 'utc' dayjs tz guess(); // create a dayjs instance with input timezone let m; if (inputtz === 'utc') { m = dayjs utc(date); } else { m = dayjs tz(date, inputtz); } // check if the date is valid if (m isvalid()) { result isvalid = true; // always include utc result formatteddates\['utc'] = m clone() utc() format(pattern); // prepare the list of additional output timezones let additionaltimezones = \[]; if (outputtz) { if (array isarray(outputtz)) { additionaltimezones = outputtz; } else if (typeof outputtz === 'string') { additionaltimezones = \[outputtz]; } else { console warn('outputtz should be a string or an array of strings representing valid iana timezones '); } } else { // default to 'local' if no outputtz is specified additionaltimezones = \['local']; } // iterate over each specified output timezone and format the date additionaltimezones foreach(tz => { let formatteddate = ''; if (tz === 'local') { const localtz = dayjs tz guess(); formatteddate = m clone() tz(localtz) format(pattern); result formatteddates\['local'] = formatteddate; } else { // validate the timezone if (dayjs tz zone(tz)) { // checks if tz is a valid timezone formatteddate = m clone() tz(tz) format(pattern); result formatteddates\[tz] = formatteddate; } else { console warn(`invalid timezone provided "${tz}" skipping this timezone `); } } }); } return result; }