Despite the COVID-19 outbreak, our team continues operating at full speed. We are always here to support and answer all your questions.

Feel free to reach out by filling this quick form.

Fill the form
Get Free Trial
  1. API reference
  2. Welcome
    1. Getting started
    2. Get Flexmonster
    3. Quick start
    4. System requirements
    5. Troubleshooting
    6. Managing license keys
    7. Migrating from WebDataRocks to Flexmonster
  3. Integration with frameworks
    1. Available tutorials
    2. Integration with Angular
    3. Integration with React
    4. Integration with Vue
    5. Other integrations
      1. Integration with Python
        1. Integration with Django
        2. Integration with Jupyter Notebook
      2. Integration with React Native
      3. Integration with AngularJS (v1.x)
      4. Integration with TypeScript
      5. Integration with R Shiny
      6. Integration with jQuery
      7. Integration with Ionic
      8. Integration with Electron.js
      9. Integration with Webpack
      10. Integration with RequireJS
  4. Connecting to Data Source
    1. Supported data sources
    2. JSON
      1. Connecting to JSON
      2. Connecting to JSON using Flexmonster Data Server
      3. Data types in JSON
    3. CSV
      1. Connecting to CSV
      2. Connecting to CSV using Flexmonster Data Server
      3. Data types in CSV
    4. Database
      1. Connecting to SQL databases
      2. Connecting to a MySQL database
      3. Connecting to a Microsoft SQL Server database
      4. Connecting to a PostgreSQL database
      5. Connecting to an Oracle database
    5. Flexmonster Data Server
      1. Introduction to Flexmonster Data Server
      2. Getting started with Flexmonster Data Server
      3. Configurations reference
      4. Data sources guide
      5. Security and authorization guide
      6. The Data Server as a DLL
        1. Getting started with the Data Server as a DLL
        2. Referencing the Data Server as a DLL
        3. Implementing the API controller
        4. Implementing the server filter
        5. Implementing the custom parser
        6. DLL configurations reference
        7. The controller's methods for request handling
      7. Troubleshooting the Data Server
    6. MongoDB
      1. Introduction to Flexmonster MongoDB Connector
      2. Getting started with the MongoDB Connector
      3. Embedding the MongoDB Connector into the server
    7. Microsoft Analysis Services
      1. Connecting to Microsoft Analysis Services
      2. Getting started with Flexmonster Accelerator
      3. Referencing the Accelerator as a DLL
      4. Configuring the authentication process
      5. Configuring a secure HTTPS connection
      6. Troubleshooting
    8. Custom data source API
      1. Introduction to the custom data source API
      2. A quick overview of a sample Node.js server
      3. A quick overview of a sample .NET Core server
      4. Implement your own server
        1. Implementing the custom data source API server
        2. Implementing filters
        3. Supporting more aggregation functions
        4. Supporting multilevel hierarchies
        5. Returning data for the drill-through view
        6. Testing your custom data source API server
    9. Elasticsearch
      1. Connecting to Elasticsearch
      2. Configuring the mapping
    10. Pentaho Mondrian
      1. Connecting to Pentaho Mondrian
      2. Getting started with the Accelerator
      3. Configuring Mondrian roles
      4. Configuring username/password protection
      5. Configuring a secure HTTPS connection
      6. Troubleshooting
    11. Connecting to other data sources
  5. Security
    1. Security in Flexmonster
    2. Security aspects of connecting to an OLAP cube
      1. Ways of connecting to an OLAP cube
      2. The data transfer process
      3. Data security
      4. Data access management
  6. Configuring report
    1. What is a report
    2. Data source
    3. Slice
    4. Options
    5. Mapping
    6. Number formatting
    7. Conditional formatting
    8. Set the report for the component
    9. Get the report from the component
    10. Date and time formatting
    11. Configuring global options
    12. Export and print
    13. Calculated values
    14. Custom sorting
  7. Charts
    1. Available tutorials
    2. Flexmonster Pivot Charts
    3. Integration with Highcharts
    4. Integration with amCharts
    5. Integration with Google Charts
    6. Integration with FusionCharts
    7. Integration with any charting library
  8. Customizing
    1. Available tutorials
    2. Customizing the Toolbar
    3. Customizing appearance
    4. Customizing the context menu
    5. Customizing the grid
    6. Customizing the pivot charts
    7. Localizing the component
  9. Updating to the latest version
    1. Updating to the latest version
    2. Release notes
    3. Migration guide from 2.7 to 2.8
    4. Migration guide from 2.6 to 2.7
    5. Migration guide from 2.5 to 2.6
    6. Migration guide from 2.4 to 2.5
    7. Migration guide from 2.3 to 2.4
    8. Migration guide from 2.2 to 2.3
  10. Flexmonster CLI Reference
    1. Overview
    2. Troubleshooting the CLI
    3. flexmonster create
    4. flexmonster add
    5. flexmonster update
    6. flexmonster version
    7. flexmonster help
  11. 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", and "api" data source types.

If your data source type is "microsoft analysis services", you can configure date and time formatting inside the cube and apply it to your data.

If your data source type is "elasticsearch", follow this guide to configure date and time formatting.

Follow the steps below to format your date and time fields.

Step 1. Learn about input date and time formats

Flexmonster supports different input formats for date and time fields:

Step 1.1. Input date format

Input date format depends on your data source:

CSV, JSON, and Flexmonster Data Server

For JSON, CSV, and Flexmonster Data Server data sources, the component supports ISO 8601 as an input date format: 

  • "2021-05-25" – Date.
  • "2021-05-25T21:30:00" – Date and time.
  • "2021-05-25T21:30:00+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-05-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-05-25" will be "1621900800" in the Unix timestamp format.

Step 1.2. Input time format

Time should be specified in seconds when using "json", "csv", or "api" data source types. Here is how it can be done in a JSON data source:

{
"Time": 121
}

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.

The first way is to use the defaultDateType option. It allows overriding the default date type for "json" and "csv" data source types:

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

Another way to define the field type is using the Mapping Object. It provides the following types for date and time 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.
    Note that Flexmonster does not display time for "date string" fields (i.e., "05/25/2021T21:30:00" will be displayed as "05/25/2021"). To display the time part of a date, use the "datetime" type for the field.
    See how to format the “date string” fields.
  • "datetime" – You can select fields of this type for values and apply min, max, count, and distinctcount aggregations to them.
    See how to format the “datetime” fields.
  • "time" – The field stores time. See how to format the “time” fields.

Here is an example of setting the type for a date field using the Mapping Object:

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.

Step 3. Set date format

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:

Step 3.1. Format fields of the “date string” type

To format fields of the "date string" type, use the datePattern property of the Options Object. Its default pattern string is "dd/MM/yyyy".

Here is how the datePattern can be specified in the 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:

  • "yyyy-MM-dd". Example of a formatted date: 2021-05-25.
  • "MMMM d, yyyy". Example of a formatted date: May 25, 2021.

Step 3.2. Format fields of the “datetime” type

To format fields of the "datetime" type, use the dateTimePattern property of the Options Object. Its default pattern string is "dd/MM/yyyy HH:mm:ss".

Here is how the dateTimePattern can be specified in the 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:

  • "yyyy-MM-dd". Example of a formatted date: 2021-05-25.
  • "yyyy-MM-dd'T'HH:mm:ss". Example of a formatted date: 2021-05-25T21:30:00.
  • "MMMM d, yyyy". Example of a formatted date: May 25, 2021.
  • "h:mm TT". Example of a formatted date: 9:30 PM.
  • "HH:mm:ss". Example of a formatted date: 21:30:00.
  • "h:mm:ss TT". Example of a formatted date: 9:30:00 PM.

Step 3.3. Format fields of the “time” type

To format fields of the "time" type, use the timePattern property of the Options Object. Its default pattern string is "HH:mm:ss".

Here is how the timePattern can be specified in the 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:

  • "HH:mm:ss". Example of a formatted time: 21:30:00.
  • "h:mm TT". Example of a formatted time: 9:30 PM.
  • "h:mm:ss TT". Example of a formatted time: 9:30:00 PM.

Step 3.4. Format specific fields

Formatting specified in the Options Object 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 Mapping Object.

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:

mapping: {
"Date": {
        type: "date string",
        format: "MM/dd/yyyy"
    }
}

Try a live sample on JSFiddle.

As you can see, datePattern is not applied to the Date field since it has the format property in the mapping.

Step 4. Manage time zones

By default, Flexmonster uses the user’s local time zone in dates. 

To use another 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. You can set a time zone in both datePattern and dateTimePattern options.

Setting a time zone affects only date representation in the component; the original data is not changed. Check out a live sample on JSFiddle.

If you need to set a time zone for hierarchical dates (i.e., for "date", "year/month/day", and "year/quarter/month/day" date types), use the options.dateTimezoneOffset property. See a live demo on JSFiddle.

What’s next?

You may be interested in the following articles: