What is a DateTime?

You are currently viewing What is a DateTime?

Overview

A DateTime data type represents and instance in time, typically expressed as a date and the time of day. 

Whenever you are looking to store or modify a date within an automation, use the DateTime data type. Unmodified, DateTime data types look like the below, but can be modified and manipulated in various ways to become applicable for many needs.

01/21/2021 11:05:36

DateTime objects have extensive uses for almost any type of application, ranging from appending dates to sales invoices, tracking the time it takes a robot to run, or even matching dates between systems.

The default and the lowest value of a DateTime object is January 1, 0001, 00:00:00 A.M and the maximum value can be December 31, 9999 11:59:59 P.M.

Modifying a DateTime data type is more complex than just adding (+) or subtracting (-) two DateTime objects. You need to use the applicable Methods and Properties, discussed here, to modify DateTime objects correctly for each application. The specific Methods and Properties applicable to DateTime objects are shown below with examples of each.

Another unique power of DateTime objects is its ability to be modified by using the ToString Method. When converting a DateTime into a String, there are many custom or default ways it can be displayed. Say you want to display just the date, or just the time, or you only want the year? This can all be accomplished using the ToString method as discussed below.

What can you do with DateTime?

Below are some great examples of the use of Objects:

  • Append dates to sales invoices
  • Track the time it takes for a robot job to run
  • Compare dates between systems

Commonly Used Properties

The following are some commonly used properties for DateTime. A property is a characteristic or attribute of an object (i.e. DateTime). To learn more about properties, click here

For all examples below, we will be using the following DateTime variable, dtVar, which has been assigned as January 21, 2021, 11:05:36 A.M.

Now - Gets the current date & time.

Example Property Example Return Return Type
dtVar.Now
02/01/2021 21:06:55
DateTime

UtcNow - Gets the current date & time in UTC.

Example Property Example Return Return Type
dtVar.UtcNow
02/02/2021 02:06:55
DateTime

Today - Gets the current date.

Example Property Example Return Return Type
dtVar.Today
02/01/2021 00:00:00
DateTime

Date - Gets the date.

Example Property Example Return Return Type
dtVar.Date
01/21/2021 00:00:00
DateTime

Day - Gets the day of the month (1-31).

Example Property Example Return Return Type
dtVar.Day
21
Integer (Int32)

DayOfWeek - Gets the day of the week (Monday - Sunday).

Example Property Example Return Return Type
dtVar.DayOfWeek
Thursday
DayOfWeek

DayOfYear - Gets the day of the year (1-365).

Example Property Example Return Return Type
dtVar.DayOfYear
21
Integer (Int32)

Hour - Gets the hour (1-24).

Example Property Example Return Return Type
dtVar.Hour
11
Integer (Int32)

Millisecond - Gets the milliseconds (0-60).

Example Property Example Return Return Type
dtVar.Millisecond
36
Integer (Int32)

Minute - Gets the minutes (0-60).

Example Property Example Return Return Type
dtVar.Minute
5
Integer (Int32)

Month - Gets the month (1-12).

Example Property Example Return Return Type
dtVar.Month
1
Integer (Int32)

Second - Gets the seconds(0-60).

Example Property Example Return Return Type
dtVar.Second
36
Integer (Int32)

Year - Gets the year.

Example Property Example Return Return Type
dtVar.Year
2021
Integer (Int32)

Commonly Used Methods

The following are some commonly used methods for DateTime. Methods are actions that objects (i.e. DateTime) can perform. To learn more about methods, click here

For all examples below, we will be using the following DateTime variable, dtVar, which has been assigned as January 21, 2021, 11:05:36 A.M.

Compare(DateTime,DateTime) - Compares 2 dates and returns 0 if they match, a negative integer if the first date is earlier than the second, and a positive integer if the first date is later than the second.

Example Method Example Return Return Type
DateTime.Compare(dtVar,dtVar)
0
Integer (Int32)

CompareTo(DateTime) - Compares 2 dates and returns 0 if they match, a negative integer if the first date is earlier than the second, and a positive integer if the first date is later than the second.

Example Method Example Return Return Type
dtVar.CompareTo(dtVar)
0
Integer (Int32)

Equals(DateTime) - Compares 2 dates and returns a Boolean.

Example Method Example Return Return Type
dtVar.Equals(dtVar)
True
Boolean

DaysInMonth(Integer,Integer) - Determines the days in a specified month. First integer parameter is the year and the second parameter is the month.

Example Method Example Return Return Type
DateTime.DaysInMonth(2019,6)
30
Integer (Int32)
DateTime.DaysInMonth(2020,12)
31
Integer (Int32)

IsDaylightSavingTime - Determines if it is Daylight Saving Time.

Example Method Example Return Return Type
dtVar.IsDaylightSavingTime
False
Boolean

IsLeapYear - Determines if the date is within a leap year.

Example Method Example Return Return Type
dtVar.IsLeapYear
False
Boolean

Add(Timespan) - Adds a timespan.

Example Method Example Return Return Type
dtVar.Add(00:01:02)
01/21/2021 11:06:38
DateTime
dtVar.Add(04:01:04)
01/21/2021 15:06:40
DateTime
dtVar.Add(01:00:00:00)
01/22/2021 11:06:38
DateTime

AddDays(Double) - Add or subtract days.

Example Method Example Return Return Type
dtVar.AddDays(5)
01/26/2021 11:05:36
DateTime
dtVar.AddDays(-3)
01/18/2021 11:05:36
DateTime

AddHours(Double) - Add or subtract hours.

Example Method Example Return Return Type
dtVar.AddHours(2)
01/25/2021 13:05:36
DateTime
dtVar.AddHours(-1)
01/25/2021 10:05:36
DateTime

AddMilliseconds(Double) - Add or subtract milliseconds.

Example Method Example Return Return Type
dtVar.AddMilliseconds(5)
01/21/2021 11:05:36
DateTime
dtVar.AddMilliseconds(-2)
01/21/2021 11:05:36
DateTime

AddMinutes(Double) - Add or subtract minutes.

Example Method Example Return Return Type
dtVar.AddMinutes(20)
01/21/2021 11:25:36
DateTime
dtVar.AddMinutes(-5)
01/21/2021 11:00:36
DateTime

AddMonths(Double) - Add or subtract months.

Example Method Example Return Return Type
dtVar.AddMonths(2)
03/21/2021 11:05:36
DateTime
dtVar.AddMonths(-1)
12/21/2020 11:05:36
DateTime

AddSeconds(Double) - Add or subtract seconds.

Example Method Example Return Return Type
dtVar.AddSeconds(5)
01/21/2021 11:05:41
DateTime
dtVar.AddSeconds(-2)
01/21/2021 11:05:34
DateTime

AddYears(Double) - Add or subtract years.

Example Method Example Return Return Type
dtVar.AddYears(1)
01/21/2022 11:05:36
DateTime
dtVar.AddYears(-3)
01/21/2018 11:05:36
DateTime

Subtract(Timespan) - Subtracts a Timespan from a DateTime.

Example Method Example Return Return Type
dtVar.Subtract(01:01:01)
01/21/2021 10:04:35
DateTime
dtVar.Subtract(10:04:35)
01/21/2021 00:00:00
DateTime

ToString - Converts to a String.

Example Method Example Return Return Type
dtVar.ToString
"01/21/2021 11:05:36"
String

ToShortDateString - Convert to a Short Date String.

Example Method Example Return Return Type
dtVar.ToShortDateString
"01/21/2021"
String

ToShortTimeString - Convert to a Long Date String.

Example Method Example Return Return Type
dtVar.ToShortTimeString
"11:05"
String

ToLongDateString - Convert to a Short Time String.

Example Method Example Return Return Type
dtVar.ToLongDateString
"Thursday, 21 January 2021"
String

ToLongTimeString - Convert to a Long Time String

Example Method Example Return Return Type
dtVar.ToLongTimeString
"11:05:36"
String