This tutorial explains how to format date and time fields when using JSON, CSV, Flexmonster Data Server, MongoDB, and the custom data source API. If using another data source type, format date and time fields as follows:
Flexmonster supports different input formats for date and time fields:
Input date format depends on your data source:
For JSON and CSV data sources, the component supports the following date formats:
"2021-04-25"
- Date."2021-04-25T21:30:05"
- Date and time."2021-04-25T21:30:05+03:00"
- Date and time with a time zone."2021-04-25"
is 1619298000000
when converted to a Unix timestamp in milliseconds.Other formats aren’t officially supported and may have unexpected results.
Flexmonster Data Server supports ISO 8601 as an input date format:
"2021-04-25"
- Date."2021-04-25T21:30:05"
- Date and time."2021-04-25T21:30:05+03:00"
- Date and time with a time zone.Other formats aren’t officially supported and may have unexpected results.
When the MongoDB Connector is used, dates should be defined in the format supported by the MongoDB database. For example, you can define dates in the ISO 8601 format:
{
"Date": "2021-04-25"
}
If using your implementation of the custom data source API, dates in the initial dataset can have any format. Your custom data source API server should process and pass them to Flexmonster as Unix timestamps in milliseconds. For example, "2021-04-25"
is 1619298000000
when converted to a Unix timestamp in milliseconds.
In Microsoft Analysis Services or Elasticsearch, dates should be specified in a format supported by these data sources:
In "json"
, "csv"
, and "api"
data source types, it is possible to define time fields. They can be used to store a time interval (e.g., duration or elapsed time), which is not related to a specific date. Specify them as a number of seconds.
Here is how a time field can be specified in a JSON data source:
[
{
"Event": "Concert",
"Duration": 451515
}
]
Note that it is important to define the field’s type as "time"
explicitly. Otherwise, the component will treat this field as a numeric one.
For details on setting the field’s type, see the next step.
By default, date fields in Flexmonster have the following types:
"date"
- When using "json"
and "csv"
data source types. Fields of this type are split into three different fields: Year, Month, and Day."date string"
- When using the "api"
data source type. The component treats fields of this type as strings; they can be used in rows, columns, or report filters.Time fields have the "number"
type and are treated as numbers by default.
Based on your requirements, you can override these default types:
Setting a type for a specific date field is possible with the MappingObject. It provides the following types for date fields:
"date"
– Fields of this type are split into three different fields: Year, Month, and Day. Only for "csv"
and "json"
data source types."year/month/day"
– Fields of this type are displayed as a multilevel hierarchy with the following levels: Year > Month > Day. Only for "csv"
and "json"
data source types."year/quarter/month/day"
– Fields of this type are displayed as a multilevel hierarchy with the following levels: Year > Quarter > Month > Day. Only for "csv"
and "json"
data source types."date string"
– Fields of this type are represented as strings and can be used in rows, columns, or report filters. Members of the "date string"
fields are sorted as dates."date string"
fields are rounded down with the "1d"
interval (i.e., "04/25/2021T21:30:05"
is rounded down to "04/25/2021T00:00:00"
). If needed, you can set a custom interval for a field. Learn more about intervals: Rounding dates using the mapping.interval."date string"
fields, see the instructions below."datetime"
– You can select fields of this type for values. To format "datetime"
fields, see the instructions below.Here is an example of setting the type for a date field using the MappingObject:
report: { dataSource: { data: [ { "Date1": "2021-03-04T22:52:00", "Date2": "2021-03-04T22:52:00", "Date3": "2021-03-04T22:52:00" } ], mapping: { "Date1": { type: "date" }, "Date2": { type: "date string" }, "Date3": { type: "datetime" } } } }
You can also watch a video overview of the available date types:
By default, Flexmonster treats time fields as numbers, so it is needed to set their type explicitly. It can be done by setting the type property of the MappingObject to "time"
.
The "time"
type is used for the fields that store a time interval (e.g., duration or elapsed time), which is not related to a specific date. For information on the input time format, see the Input time format section.
Here is an example of how to define the "time"
type in the mapping:
report: {
dataSource: {
data: [
{
"Event": "Concert",
"Duration": 451515
}
],
mapping: {
"Duration": {
type: "time"
}
}
},
options: {
timePattern: "d'd' HH'h' mm'min'"
}
}
Now the Duration field will be displayed as 5d 05h 25min
.
To format "time"
fields, see this section.
To set a type for all date fields, use the defaultDateType property of the OptionsObject. It allows overriding the default date type for "json"
and "csv"
data source types:
report: { options: { defaultDateType: "date string" } }
Now the "date string"
type will be applied to all date fields in the dataset.
In Flexmonster, date fields are formatted using pattern strings.
A pattern consists of letters that are replaced with date and time values in the formatted string. For example, in the pattern "yyyy/MM"
, the "yyyy"
substring is replaced with a four-digit year followed by a "/"
character, and the "MM"
substring is replaced with a two-digit month.
You can define pattern strings for:
To format fields of the "date string"
type, use the datePattern property of the OptionsObject. Its default pattern string is "dd/MM/yyyy"
.
Here is how the datePattern
can be specified in the report:
report: {
options: {
datePattern: "yyyy.MM.dd"
},
dataSource: {
data: [
// ...
],
mapping: {
"Date": { type: "date string" }
}
}
}
Supported patterns for the "date string"
fields are the following:
d
– Day of the month. It is represented as a one- or two-digit number. For example, 2 or 18.dd
– Day of the month. It is represented as a two-digit number. For example, 02 or 18.ddd
– Day of the week. It is represented as a three-letter abbreviation of the day of the week. For example, Wed.dddd
– Day of the week. It is represented as the full name of the day of the week. For example, Wednesday.M
– Month. It is represented as a one- or two-digit number. For example, 3 or 11.MM
– Month. It is represented as a two-digit number. For example, 03.MMM
– Month. It is represented as a three-letter abbreviation of the name of the month. For example, Mar.MMMM
– Month. It is represented as the full name of the month. For example, March.yy
– Year. It is represented as a two-digit number. For example, 16.yyyy
– Year. It is represented as a four-digit number. For example, 2016.h
– Hour of the day using the 12-hour format [1 – 12]. It is represented as a one or two-digit number. For example, 1 or 12.hh
– Hour of the day using the 12-hour format [1 – 12]. It is represented as a two-digit number. For example, 01 or 12.H
– Hour of the day using the 24-hour format [0 – 23]. It is represented as a one or two-digit number. For example, 0 or 23.HH
– Hour of the day using the 24-hour format [0 – 23]. It is represented as a two-digit number. For example, 00 or 23.k
– Hour of the day using the 24-hour format [1 – 24]. It is represented as a one or two-digit number. For example, 1 or 24.kk
– Hour of the day using the 24-hour format [1 – 24]. It is represented as a two-digit number. For example, 01 or 24.m
– Minutes [0 – 59]. It is represented as a one or two-digit number. For example, 0 or 59.mm
– Minutes [0 – 59]. It is represented as a two-digit number. For example, 00 or 59.s
– Seconds [0 – 59]. It is represented as a one or two-digit number. For example, 0 or 59.ss
– Seconds [0 – 59]. It is represented as a two-digit number. For example, 00 or 59.t
– am/pm – a one-letter indicator. For example, a or p.tt
– am/pm – a two-letter indicator. For example, am or pm.T
– AM/PM – a one-letter indicator. For example, A or P.TT
– AM/PM – a two-letter indicator. For example, AM or PM.UTC:
– indicates that the UTC time zone should be used. Example format: "UTC:dd/MM/yyyy"
.GMT+-N:
– indicates the time zone to use, where N can be changed from 1 to 12. Example format: "GMT+6:dd/MM/yyyy"
.Note By default, "date string"
fields are rounded down with the "1d"
interval. As a result, the time part is always 00:00:00
(e.g., "04/25/2021T21:30:05"
is rounded down to "04/25/2021T00:00:00"
). If you want to display the actual time instead of 00:00:00
, specify a smaller interval in the mapping (e.g., with the "1h"
interval value, "04/25/2021T21:30:05"
is rounded down to "04/25/2021T21:00:00"
).
Have a look at how the datePattern
can be specified:
Pattern | Example of a formatted date |
---|---|
"yyyy-MM-dd" | 2021-05-25 |
"M/d/yy" | 4/25/21 |
"MMM d, yyyy" | Mar 25, 2021 |
"MMMM d, yyyy" | March 25, 2021 |
"dddd, MMMM d, yyyy" | Sunday, March 25, 2021 |
To format fields of the "datetime"
type, use the dateTimePattern property of the OptionsObject. Its default pattern string is "dd/MM/yyyy HH:mm:ss"
.
Here is how the dateTimePattern
can be specified in the report:
report: {
options: {
dateTimePattern: "yyyy.MM.dd HH:mm TT"
},
dataSource: {
data: [
// ...
],
mapping: {
"Date": { type: "datetime" }
}
}
}
Supported patterns for the "datetime"
fields are the following:
d
– Day of the month. It is represented as a one or two-digit number. For example, 2 or 18.dd
– Day of the month. It is represented as a two-digit number. For example, 02 or 18.ddd
– Day of the week. It is represented as a three-letter abbreviation of the day of the week. For example, Wed.dddd
– Day of the week. It is represented as the full name of the day of the week. For example, Wednesday.M
– Month. It is represented as a one or two-digit number. For example, 3 or 11.MM
– Month. It is represented as a two-digit number. For example, 03.MMM
– Month. It is represented as a three-letter abbreviation of the name of the month. For example, Mar.MMMM
– Month. It is represented as the full name of the month. For example, March.yy
– Year. It is represented as a two-digit number. For example, 16.yyyy
– Year. It is represented as a four-digit number. For example, 2016.h
– Hour of the day using the 12-hour format [1 – 12]. It is represented as a one or two-digit number. For example, 1 or 12.hh
– Hour of the day using the 12-hour format [1 – 12]. It is represented as a two-digit number. For example, 01 or 12.H
– Hour of the day using the 24-hour format [0 – 23]. It is represented as a one or two-digit number. For example, 0 or 23.HH
– Hour of the day using the 24-hour format [0 – 23]. It is represented as a two-digit number. For example, 00 or 23.k
– Hour of the day using the 24-hour format [1 – 24]. It is represented as a one or two-digit number. For example, 1 or 24.kk
– Hour of the day using the 24-hour format [1 – 24]. It is represented as a two-digit number. For example, 01 or 24.m
– Minutes [0 – 59]. It is represented as a one or two-digit number. For example, 0 or 59.mm
– Minutes [0 – 59]. It is represented as a two-digit number. For example, 00 or 59.s
– Seconds [0 – 59]. It is represented as a one or two-digit number. For example, 0 or 59.ss
– Seconds [0 – 59]. It is represented as a two-digit number. For example, 00 or 59.l
– Milliseconds. It is represented as a three-digit number. For example, 100.L
– 10 Milliseconds. It is represented as a two-digit number (rounded, if needed). For example, 10.t
– am/pm – a one-letter indicator. For example, a or p.tt
– am/pm – a two-letter indicator. For example, am or pm.T
– AM/PM – a one-letter indicator. For example, A or P.TT
– AM/PM – a two-letter indicator. For example, AM or PM.UTC:
– indicates that the UTC time zone should be used. Example format: "UTC:dd/MM/yyyy HH:mm:ss"
.GMT+-N:
– indicates the time zone to use, where N can be changed from 1 to 12. Example format: "GMT+6:dd/MM/yyyy HH:mm:ss"
.Have a look at how the dateTimePattern
can be specified:
Pattern | Example of a formatted date |
---|---|
"yyyy-MM-dd" | 2021-05-25 |
"M/d/yy, h:mm TT" | 4/25/21, 9:30 PM |
"MMM d, yyyy, h:mm:ss TT" | Mar 25, 2021, 9:30:00 PM |
"MMMM d, yyyy" | March 25, 2021 |
"dddd, MMMM d, yyyy" | Sunday, March 25, 2021 |
"h:mm TT" | 9:30 PM |
"h:mm:ss TT" | 9:30:05 PM |
To format fields of the "time"
type, use the timePattern property of the OptionsObject. Its default pattern string is "HH:mm:ss"
.
Here is how the timePattern
can be specified in the report:
report: {
options: {
timePattern: "d'd' HH'h' mm'min'"
},
dataSource: {
data: [
// ...
],
mapping: {
"Time": { type: "time" }
}
}
}
Supported patterns for the "time"
fields are the following:
d
– Days. It is represented as a one- or two-digit number. For example, 5 or 11.dd
– Days. It is represented as a two-digit number. For example, 05 or 11.H
– Hours. It is represented as a one- or two-digit number. For example, 6 or 15.HH
– Hours. It is represented as a two-digit number. For example, 06 or 15.HHH
– Hours. Displays time data in hours even when the number of hours is greater than 24. For example, 46 or 15.m
– Minutes. It is represented as a one- or two-digit number. For example, 2 or 10.mm
– Minutes. It is represented as a two-digit number. For example, 02 or 10.s
– Seconds. It is represented as a one- or two-digit number. For example, 3 or 16.ss
– Seconds. It is represented as a two-digit number. For example, 03 or 16.Have a look at how the timePattern
can be specified:
Pattern | Example of a formatted time |
---|---|
"HH:mm:ss" | 21:30:00 |
"d'd' HH'h' mm'min'" | 0d 21h 30min |
Formatting specified in the OptionsObject applies to all the fields of the respective type (e.g., all "date string"
fields are formatted according to the datePattern). To format a specific field, use the format property of the MappingObject.
It works the same way as datePattern
, dateTimePattern
, and timePattern
options and overrides their values. Here is an example of how to define formatting for a certain field:
report: {
dataSource: {
data: [
// ...
],
mapping: {
"OrderDate": {
type: "date string",
format: "MM/dd/yyyy"
}
}
}
}
In the example above, datePattern
is not applied to the OrderDate
field since it has the format
property in the mapping.
By default, Flexmonster uses the user’s local time zone in dates.
For example, your dataset contains a date with the GMT+2 time zone: "2021-04-25T21:30:05+02:00"
. Flexmonster will display this date according to your local time zone
Live example.
If needed, you can set another time zone for your date fields.
Note Setting a time zone in the component affects only date representation; the original data is not changed.
You can set time zones:
To set a timezone for all date fields, use the dateTimezoneOffset property. It is a number that represents the difference in hours between the needed time zone and UTC.
For example, the GMT-5 time zone can be set like this:
report: {
options: {
dateTimezoneOffset: -5
},
dataSource: {
// ...
}
}
You can set a time zone for fields of a certain type as follows:
"date string"
type, set a time zone using the datePattern option."GMT+-N:"
at the beginning of the pattern (e.g., "GMT+6:dd/MM/yyyy"
). N
can be changed from 1
to 12
."datetime"
type, set a time zone using the dateTimePattern option."GMT+-N:"
at the beginning of the pattern (e.g., "GMT+6:dd/MM/yyyy HH:mm:ss"
). N
can be changed from 1
to 12
.You can set a time zone for a specific field using the format property of the MappingObject. This option is available for fields of the "date string"
and "datetime"
types.
To set a time zone, include "GMT+-N:"
at the beginning of the pattern (e.g., "GMT+6:dd/MM/yyyy HH:mm:ss"
). N
can be changed from 1
to 12
.
Live example
Note A time zone from the format property will override time zones from datePattern, dateTimePattern, and dateTimezoneOffset properties.
If needed, dates in Flexmonster can be rounded down with the mapping.interval property. This feature can be used to group dates by a certain interval on the pivot table.
Note Fields of the "date string"
type have the "1d"
interval by default.
In the code snippet below, all dates have the same day part, but different time parts. The "1d"
interval will round all dates to 2021-04-25T00:00:00
:
report: { dataSource: { data: [ { "DeliveryDate": "2021-04-25T21:30:05", "Price": 200 }, { "DeliveryDate": "2021-04-25T23:55:00", "Price": 150 } ], mapping: { "Date": { interval: "1d", type: "datetime" } } } }
You may be interested in the following articles: