Skip to content

k.utils.date ​

Date time and formatting

Execution ContextPrerequisitesTypical Use
KScript API, Page / View server scriptsNoneFormatting timestamps and computing relative expiration dates

k.utils.date reads the current time and provides DateTime arithmetic and formatting.

Quick Start ​

js
k.api.get("time", () => {
  const now = k.utils.date.now
  const tomorrow = k.utils.date.addDays(now, 1)
  return {
    now: k.utils.date.formate(now, "yyyy-MM-dd HH:mm:ss"),
    tomorrow: k.utils.date.formate(tomorrow, "yyyy-MM-dd")
  }
})

Quick Reference ​

Method / PropertyDescriptionReturns
utcNowCurrent UTC date/timeDate
nowCurrent local date/timeDate
formate(dateTime[, formate])Format date string (defaults to yyyy-MM-dd HH:mm:ss)string
addSeconds(dateTime, seconds)Add or subtract seconds from a DateDate
addMinutes(dateTime, minutes)Add or subtract minutes from a DateDate
addHours(dateTime, hours)Add or subtract hours from a DateDate
addDays(dateTime, days)Add or subtract days from a DateDate
addMonths(dateTime, months)Add or subtract months (integer values)Date
addYears(dateTime, years)Add or subtract years (integer values)Date

Properties ​

Parameters: None.

Returns: Date.

PropertyTypeDescription
utcNowDateUTC current time
nowDateLocal current time
js
const utc = k.utils.date.utcNow
const local = k.utils.date.now

formate() ​

Format a date (the method name is spelled formate in the runtime).

ParameterTypeRequiredDescription
dateTimeDateYesDate/time to format
formatestringNoFormat string; defaults to yyyy-MM-dd HH:mm:ss

Returns: string.

js
const s = k.utils.date.formate(k.utils.date.now, "yyyy-MM-dd")
const s2 = k.utils.date.formate(k.utils.date.now) // Default format

addSeconds/addMinutes/addHours/addDays/addMonths/addYears ​

Offset the given DateTime and return a new Date.

ParameterTypeRequiredDescription
dateTimeDateYesBase date/time
valuenumberYesOffset amount; addMonths and addYears use integer values

Returns: Date, the offset date/time.

js
const tomorrow = k.utils.date.addDays(k.utils.date.now, 1)
const expiresAt = k.utils.date.addMinutes(k.utils.date.now, 10)

TypeScript Definitions ​

ts
interface KDate {
  utcNow: Date;
  now: Date;
  formate(dateTime: Date, formate: string): string;
  formate(dateTime: Date): string;
  addSeconds(dateTime: Date, seconds: number): Date;
  addMinutes(dateTime: Date, minutes: number): Date;
  addHours(dateTime: Date, hours: number): Date;
  addDays(dateTime: Date, days: number): Date;
  addMonths(dateTime: Date, months: number): Date;
  addYears(dateTime: Date, years: number): Date;
}