1. Home
  2. Docs
  3. Chart Axis And Labels
  4. Date, Time and Format

Date, Time and Format

In many cases we would like to show dates or times in the chart axis. This can be done by setting the axis format using the Inspector :

  • Date means only a date is shown , for example 1/1/2019
  • Time means only the time is shown , for example 1:00 AM
  • Date Time shows both date and time , for example 1/1/2019 1:00 AM
  • Number shows a plain number. you can control how many fraction digits are shown by setting Fraction Digits

Passing dates into methods and the view portion

Under the hood , Graph and Chart uses only doubles to specify values. When a date is passed to graph and chart , it is converted to a double. Most methods of graph and chart contain overloads that allow you to use dates without thinking about converting them. You do have to know how to use dates and times in C# . Check out the following useful links:

When writing dates and times in the inspector , you can specify their value in seconds.

  • When setting a date , simple use it’s Unix TimeStamp so for example the date 1/1/2019 can be set by typing
    1546300800
  • When setting a time span , simply write it in seconds. For example an hour is 3600

if you need to convert numbers and dates from within the code, You can use the class ChartDateUtillity

graph.DataSource.HorizontalViewSize =  ChartDateUtility.TimeSpanToValue(TimeSpan.FromHours(5));
graph.HorizontalScrolling =  ChartDateUtility.DateToValue(new DateTime(2019, 1, 1));  
DateTime d = ChartDateUtility.ValueToDate(g.HorizontalScrolling);

Formating Dates And Times

You can set the format of dates and times using a lambda hook, as in the following code snippet :

chart.CustomDateTimeFormat = (date) => { /*your date formatting code goes here/* };

//for example
chart.CustomDateTimeFormat = (date) => { return date.ToString("MMM"); }; // show only month name

In order for this to work , you must set your axis to DateTime format :

you can view this in the tutorials folder of the asset under misc/customFormats. learn more about date formats in c#