new Utils/Date()
Extends moment.js with additional features.
Also provides a backwards compatibility layer for existing code; however, this is deprecated and you are strongly advised not to use it.
Classes
Methods
-
<static> compare(date1, date2)
-
Performs a three-way comparison between two dates. Compatible with
Array.sort
and similar functions.Parameters:
Name Type Description date1
string The first date to compare.
date2
string The second date to compare.
Throws:
if any of the given dates are invalid.
Returns:
The result of comparing
date1
todate2
:-1
if date1 < date20
if date1 = date21
if date1 > date2
- Type
- number
-
<static> format(date, format)
-
Formats JavaScript Date objects into date strings.
Parameters:
Name Type Description date
Date The date to format.
format
string Date format string such as 'Y-m-d H:i:s'.
- Deprecated:
-
- since 7.2.0.
Returns:
The formatted date string.
- Type
- string
-
<static> getRelativeTimeLabel(date)
-
Converts a Date object into a relative time.
Parameters:
Name Type Description date
Date Date object to convert.
- Deprecated:
-
- since 7.2.0.
Returns:
Object containing relative time key string and value suitable for passing to a Handlebars template.
- Type
- Object
-
<static> getUserDateFormat( [user])
-
Gets the date format preference for the given user.
Parameters:
Name Type Argument Default Description user
Data/Bean | Object <optional>
Core.User The user whose date format you want.
Returns:
A format string for the user's preferred date format.
- Type
- string
-
<static> getUserTimeFormat( [user])
-
Gets the time format preference for the given user.
Parameters:
Name Type Argument Default Description user
Data/Bean | Object <optional>
Core.User The user whose time format you want.
Returns:
A format string for the user's preferred time format.
- Type
- string
-
<static> guessFormat(date)
-
Guesses the format of a date string.
Parameters:
Name Type Description date
string A date string.
- Deprecated:
-
- since 7.2.0.
Returns:
A string encoding the date/time format used by
date
, orfalse
ifdate
is invalid.- Type
- string | boolean
-
<static> isIso(val)
-
Determines if a date string is in ISO 8601 format.
Parameters:
Name Type Description val
string The date string to check.
- Deprecated:
-
- since 7.2.0.
Returns:
true
ifval
is compliant with ISO 8601.- Type
- boolean
-
<static> parse(date [, oldFormat])
-
Parses date strings into JavaScript Dates.
Parameters:
Name Type Argument Description date
string The date string to parse.
oldFormat
string <optional>
Date format string. If not specified, this function will guess the date format.
- Deprecated:
-
- since 7.2.0.
Returns:
A Date object corresponding to the given
date
.- Type
- Date
-
<static> parseDisplayDefault(displayDefault [, now])
-
Parses the
display_default
property which the server returns for datetimecombo metadata, etc. (for example, if the user sets the default in Studio).Examples:
const DateManager = require('utils/date'); DateManager.parseDisplayDefault('+1 day&06:00pm'); DateManager.parseDisplayDefault('-1 day&06:00pm'); DateManager.parseDisplayDefault('+1 week&06:00pm'); DateManager.parseDisplayDefault('+1 month&06:00pm'); DateManager.parseDisplayDefault('+1 year&06:00pm'); DateManager.parseDisplayDefault('now&06:00pm'); DateManager.parseDisplayDefault('next monday&06:00pm'); DateManager.parseDisplayDefault('next friday&06:00pm'); DateManager.parseDisplayDefault('first of next month@06:00pm');
Parameters:
Name Type Argument Default Description displayDefault
string The value of the
display_default
property.now
Date <optional>
new Date() An optional date to use as a point of reference (since we essentially convert "+1 day", etc., to an adjusted date). This is mainly for testing odd dates like adding a month specifically to January 31, etc.
- Deprecated:
-
- since 7.2.0.
Returns:
The date created by evaluating
displayDefault
relative tonow
.- Type
- Date | undefined
-
<static> parseFormat(format)
-
Parses a date format string into each of its individual representations. Supports only the options that are supported by the
date.format
function.Parameters:
Name Type Description format
string A date format string such as 'Y-m-d H:i:s'.
- Deprecated:
-
- since 7.2.0.
Returns:
Object with properties representing each piece of a format.
- Type
- Object
-
<static> roundTime(date)
-
Rounds a date to the nearest 15 minutes.
Parameters:
Name Type Description date
Date A date to round.
- Deprecated:
-
- since 7.2.0.
Returns:
Rounded date.
- Type
- Date
-
<static> stripIsoTimeDelimterAndTZ(value)
-
Strips out the 'T' and either the 'Z' or +00:00 from a date string.
Parameters:
Name Type Description value
string The date string to strip in ISO 8601 format.
- Deprecated:
-
- since 7.2.0.
Returns:
The result of removing the time delimiter and time zone indicator from
value
.- Type
- string
-
<static> toDatepickerFormat(formatSpec)
-
Converts a PHP date format string to its Bootstrap Datepicker equivalent.
Parameters:
Name Type Description formatSpec
string The original SugarCRM (PHP) date format.
Returns:
Format spec passed in normalized for the Bootstrap Datepicker widget. If falsy, returns empty string.
- Type
- string
-
<static> toUTC(date)
-
Converts the date from local time to UTC.
Parameters:
Name Type Description date
Date A UTC date.
- Deprecated:
-
- since 7.2.0.
Returns:
The given
date
as milliseconds since the Unix epoch in UTC.- Type
- number
-
<static> UTCtoLocalTime(date)
-
Converts a UTC date to a local time date.
Parameters:
Name Type Description date
Date A UTC date.
- Deprecated:
-
- since 7.2.0.
Returns:
Date converted to local time.
- Type
- Date
-
<static> UTCtoTimezone(date, offset)
-
Converts a UTC date to a date in the timezone represented by the offset.
Parameters:
Name Type Description date
Date A UTC date.
offset
number The timezone's UTC offset in hours.
- Deprecated:
-
- since 7.2.0.
Returns:
Converted date.
- Type
- Date
-
formatServer( [dateOnly])
-
Formats this date to a string according to the server date format and locale.
Parameters:
Name Type Argument Default Description dateOnly
boolean <optional>
false Pass
true
to only get the date.Returns:
This date formatted according to the server date format.
- Type
- string
-
formatUser( [dateOnly] [, user])
-
Formats this date to a string based on user preferences.
Parameters:
Name Type Argument Default Description dateOnly
boolean <optional>
false Pass
true
to only get the date.user
Data/Bean | Object <optional>
Core.User The user bean or the current logged in user object.
Returns:
The formatted date based on
user
's preference.- Type
- string
-
isBetween(startDate, endDate [, inclusive])
-
Returns
true
if this date is between the givenstartDate
andendDate
.Parameters:
Name Type Argument Default Description startDate
string The start date of the period to test.
endDate
string The end date of the period to test.
inclusive
boolean <optional>
true If
true
, includestartDate
andendDate
in the range of acceptable dates.Returns:
true
if this date is betweenstartDate
andendDate
;false
otherwise.- Type
- boolean