Pular para o conteúdo principal

Datetime

A sub namespace of Utils, exclusive for date operations


convertMinutesToDate

Description

This method can be used to convert minutes into a date.

Method(s)

function convertMinToDate(totalMinutes: number): Date
ParameterTypeRequiredDefaultsDescription
totalMinutesnumbertrueMinutes to convert to date

Basic Usage

SW.Utils.Datetime.convertMinutesToDate(63984503);

Response

"Thu Aug 26 2021 16:23:00 GMT+0100 (Western European Summer Time)";

convertToUtc

Description

This method can be used to convert a local date to utc date.

Method(s)

function convertToUtc(date: Date): Date
ParameterTypeRequiredDefaultsDescription
dateDatetrueDate to convert to utc

Basic Usage

SW.Utils.Datetime.convertToUtc(
"Thu Aug 26 2021 16:23:00 GMT+0100 (Western European Summer Time)"
);

Response

"Thu Aug 26 2021 15:23:00 GMT+0100 (Western European Summer Time)";

convertUtcToLocal

Description

This method can be used to convert utc date to a local date.

Method(s)

function convertUtcToLocal(utcDate: Date): Date
ParameterTypeRequiredDefaultsDescription
utcDateDatetrueDate to convert to local

Basic Usage

SW.Utils.Datetime.convertUtcToLocal("2021-08-25T15:23:26.222Z");

Response

"Wed Aug 25 2021 16:23:26 GMT+0100 (Western European Summer Time)";

Example

{
colCount: 2,
dataField: "CreatedOnUtc",
editorType: "dxDateBox",
label: {
text: "Created On"
},
editorOptions: {
type: "datetime",
displayFormat: "dd/MMM/yyyy HH:mm",
onInitialized: function onInit(e) {
var context = workspaceContext;
var date = SW.Utils.Datetime.convertUtcToLocal(context.currentDocument.Dto.Request.CreatedOnUtc);
e.component.option('value', date);
}
}
}

img-box-shadow-200


formatDateToString

Description

This method can be used to convert date to string format "yyyy-MM-dd" or "yyyy-MMMM-dd, hh:mm".

Method(s)

function formatDateToString(date: Date, params: { includeTime?: boolean } = { includeTime: false }): string
ParameterTypeRequiredDefaultsDescription
utcDateDatetrueDate to convert to local
includeTimebooleanfalsefalseIf to convert with or without time

Basic Usage

var date = "Wed Aug 25 2021 16:23:26 GMT+0100 (Western European Summer Time)";
SW.Utils.Datetime.formatDateToString(date);
SW.Utils.Datetime.formatDateToString(date, { includeTime: true });

Response

"2021-08-25";
"25 August 2021, 16:23";

getDateTimeFormat

Description

This method can be used to get the default datetime format.

Method(s)

function getDateTimeFormat(): string

Basic Usage

SW.Utils.Datetime.getDateTimeFormat();

Response

"dd/MMM/yyyy HH:mm";

getMinutesInHHMMformat

Description

This method can be used to convert minutes to format HH:mm.

Method(s)

function getMinutesInHHMMformat(minutes: number): string
ParameterTypeRequiredDefaultsDescription
minutesnumbertrueMinutes to convert to HH:mm

Basic Usage

SW.Utils.Datetime.getMinutesInHHMMformat(1234);

Response

"20:34";

getTimeRange

Description

This method can be used to get a end date by giving a start date and a range.

Method(s)

1   function getTimeRange(value: Date, shift: DateShift,
params: {
centerAroundValue?: boolean
} = {
centerAroundValue: false
}
): { startDate: Date, endDate: Date }
ParameterTypeRequiredDefaultsDescription
valueDatetrueStart date in range
shiftDateShifttrueRange
centerAroundValuebooleanfalseIf returns exact minutes and seconds

Basic Usage

var date = "Wed Aug 25 2021 16:23:26 GMT+0100 (Western European Summer Time)";
SW.Utils.Datetime.getTimeRange(date, { unit: "HOUR", value: 1, id: "HOUR" });
SW.Utils.Datetime.getTimeRange(
date,
{ unit: "HOUR", value: 1, id: "HOUR" },
{ centerAroundValue: true }
);

Response

{
startDate: "Wed Aug 25 2021 16:00:00 GMT+0100 (Western European Summer Time)",
endDate: "Wed Aug 25 2021 17:00:00 GMT+0100 (Western European Summer Time)"
}
{
startDate: "Wed Aug 25 2021 16:23:26 GMT+0100 (Western European Summer Time)",
endDate: "Wed Aug 25 2021 17:23:26 GMT+0100 (Western European Summer Time)"
}

timeAgo

Description

This method can be used to get how much time has passed since a given date.

Method(s)

1   function timeAgo(dateString: string,
params: {
unitOfTime?: "seconds" | "minutes" | "hours" | "days" | "months" | "years"
} = {}
): string | number
ParameterTypeRequiredDefaultsDescription
dateStringstringtrueDate in string
unitOfTime"seconds" | "minutes" | "hours" | "days" | "months" | "years"falseUnit of the result

Basic Usage

var date = "Wed Aug 25 2021 16:23:26 GMT+0100 (Western European Summer Time)";
SW.Utils.Datetime.timeAgo(date);
SW.Utils.Datetime.timeAgo(date, { unitOfTime: "minutes" });

Response

"a minute ago";
1;