Get Free Trial
  1. API reference
Connecting to Data Source
Configuring the component
  • Documentation for older versions
  • Table of contents

    Date and time formatting

    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:

    • For Microsoft Analysis Services, Flexmonster uses date formatting configured inside the cube.
    • For Elasticsearch, you can configure date and time formatting using the mapping.format property.

    See the sections below to format date and time fields in Flexmonster:

    Basic date and time formatting

    Step 1. Specify your date and time fields in the supported format

    Flexmonster supports different input formats for date and time fields:

    Input date format

    Input date format depends on your data source:

    JSON and CSV

    For JSON and CSV data sources, the component supports the following date formats:

    1. ISO 8601. For example:
      • "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.
    2. Unix timestamp. For example, "2021-04-25" will be 1619298000 in the Unix timestamp format.
      Note that it is important to assign the date type to the Unix timestamp field explicitly. Otherwise, the component will treat this field as a numeric one. For details on setting the field’s type, see the next step.

    Other formats aren’t officially supported and may have unexpected results.

    Flexmonster Data Server

    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.

    MongoDB

    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"
    }

    Custom data source API

    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. For example, "2021-04-25" will be 1619298000 in the Unix timestamp format.

    In Microsoft Analysis Services or Elasticsearch, dates should be specified in a format supported by these data sources:

    Input time format

    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.

    Step 2. Choose a type for your date and time fields

    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:

    Set a type for all date fields in the dataset (only for JSON and CSV)

    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.

    Try a live sample on JSFiddle.

    Set a type for a specific date field

    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.
      For information on the input date format, see Step 1.1. Input date format.
    • "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.
      For information on the input date format, see Step 1.1. Input date format.
    • "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.
      For information on the input date format, see Step 1.1. Input date format.
    • "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.
      Note that by default, "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.
      To format "date string" fields, see the instructions below.
      For information on the input date format, see Step 1.1. Input date format.
    • "datetime" – You can select fields of this type for values. To format "datetime" fields, see the instructions below.
      For information on the input date format, see Step 1.1. Input date format.

    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"
                }
            }
        }
    }

    See a live sample on JSFiddle.

    Set a type for a specific time field

    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.

    See the full example on JSFiddle.

    To format "time" fields, see this section.

    Step 3. Format date and time fields

    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:

    Format fields of the “date string” type

    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" }
            }
        }
    }

    Test or modify this example on JSFiddle.

    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.
    • 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".

    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

    Format fields of the “datetime” type

    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" }
            }
        }
    }

    See the full code on JSFiddle.

    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

    Format fields of the “time” type

    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" }
            }
        }
    }

    Check out a full example on JSFiddle.

    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

    Format specific fields (overrides other patterns)

    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"
                }
            }
        }
    }

    Try a live sample on JSFiddle.

    In the example above, datePattern is not applied to the OrderDate field since it has the format property in the mapping.

    Managing time zones

    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. Try a live demo on JSFiddle.

    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:

    Set a time zone for all date fields

    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: {
            ...
        }
    }

    See an example on JSFiddle.

    Set a time zone for fields of a certain date type

    You can set a time zone for fields of a certain type as follows:

    • For fields of the "date string" type, set a time zone using the datePattern option.
      To set a time zone, include "GMT+-N:" at the beginning of the pattern (e.g., "GMT+6:dd/MM/yyyy"). N can be changed from 1 to 12. See an example on JSFiddle.
      Note that a time zone from the datePattern will override a time zone from the dateTimezoneOffset.
    • For fields of the "datetime" type, set a time zone using the dateTimePattern option.
      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. See an example on JSFiddle.
      Note that a time zone from the dateTimePattern will override a time zone from the dateTimezoneOffset.
    • For all other date types, use the approach described in step 4.1.

    Set a time zone for specific fields (overrides other time zones)

    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. See an example on JSFiddle.

    Note A time zone from the format property will override time zones from datePattern, dateTimePattern, and dateTimezoneOffset properties.

    Rounding dates using the mapping.interval

    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"
                }
            }
        }
    }

    See an example on JSFiddle.

    What’s next?

    You may be interested in the following articles: