Date and Time functions - Splunk Documentation (2024)

The following list contains the functions that you can use to calculate dates and time.

For information about using string and numeric fields in functions, and nesting functions, see Overview of SPL2 eval functions.

now()

This function takes no arguments and returns the time that the search was started.

Usage

The now() function is often used with other data and time functions.

The time returned by the now() function is represented in UNIX time, or in seconds since Epoch time.

When used in a search, this function returns the UNIX time when the search is run. If you want to return the UNIX time when each result is returned, use the time() function instead.

You can use this function with the eval and where commands, in the WHERE clause of the from command, and as part of evaluation expressions with other commands.

Basic example

The following example determines the UNIX time value of the start of yesterday, based on the value of now().

... | eval n=relative_time(now(), "-1d@d")

Extended example

If you are looking for events that occurred within the last 30 minutes you need to calculate the event hour, event minute, the current hour, and the current minute. You use the now() function to calculate the current hour (curHour) and current minute (curMin). The event timestamp, in the _time field, is used to calculate the event hour (eventHour) and event minute (eventMin). For example:

... earliest=-30d | eval eventHour=strftime(_time,"%H") | eval eventMin=strftime(_time,"%M") | eval curHour=strftime(now(),"%H") | eval curMin=strftime(now(),"%M") | where (eventHour=curHour and eventMin > curMin - 30) or (curMin < 30 and eventHour=curHour-1 and eventMin>curMin+30) | bin _time span=1d | timechart count() by _time

relative_time(<time>,<specifier>)

This function takes a UNIX time and a relative time specifier and returns the UNIX time value of the specifier applied to the time.

Usage

You can use this function with the eval and where commands, in the WHERE clause of the from command, and as part of evaluation expressions with other commands.

Basic examples

The following example determines the UNIX time value of the start of yesterday, based on the value of now().

... | eval n=relative_time(now(), "-1d@d")

The following example specifies an earliest time of 2 hours ago snapped to the hour and a latest time of 1 hour ago snapped to the hour:

... | where _time>relative_time(now(), "-2h@h") AND _time<relative_time(now(), "-1h@h")

strftime(<time>,<format>)

This function takes a UNIX time value and renders the time as a string using the format specified. The UNIX time must be in seconds. Use the first 10 digits of a UNIX time to use the time in seconds.

Usage

You can use this function with the eval and where commands, in the WHERE clause of the from command, and as part of evaluation expressions with other commands.

Converting time into seconds

If the time is in milliseconds, microseconds, or nanoseconds you must convert the time into seconds. You can use the pow function to convert the number.

  • To convert from milliseconds to seconds, divide the number by 1000 or 10^3.
  • To convert from microseconds to seconds, divide the number by 10^6.
  • To convert from nanoseconds to seconds, divide the number by 10^9.

The following search uses the pow function to convert from nanoseconds to seconds:

| from [{ }] | eval StartTimestamp="1566554581000"| eval starttime=strftime(StartTimestamp/pow(10,9),"%Y-%m-%dT%H:%M:%S.%Q")


The results look like this:

StartTimeStamp_timestarttime
15665545810002019-08-23 10:03:012019-08-23T03:03:01.000

In these results the _time value is the date and time when the search was run.


For a complete list and descriptions of the format options you can use, see Using time variables in the SPL2 Search Manual.

Basic example

The following example returns the hour and minute from the _time field.

...| eval hour_min=strftime(_time, "%H:%M")

If the _time field value is 2022-08-10 11:48:23, the value returned in the hour_min field is 11:48.

Extended example

The following example creates a single result using the from command.

| from [{ }]

For example:

_time
2022-08-22 14:00:15

The _time field is stored in UNIX time, even though it displays in a human readable format. To convert the UNIX time to some other format, you use the strftime function with the date and time format variables. The variables must be in quotations marks.

For example, to return the week of the year that an event occurred in, use the %V variable.

| from [{ }] | eval week=strftime(_time,"%V")

The results are show the value 34 for week.

_timeweek
2022-08-22 14:00:1534

To return the date and time with subseconds and the time designator (the letter T) that precedes the time components of the format, use the %Y-%m-%dT%H:%M:%S.%Q variables. For example:

| from [{ }] | eval mytime=strftime(_time,"%Y-%m-%dT%H:%M:%S.%Q")

The results are:

_timemytime
2022-08-22 14:00:152022-08-22T14:00:15.000

strptime(<str>, <format>)

Takes a human readable time, represented by a string, and parses the time into a UNIX timestamp using the format you specify. You use date and time variables to specify the format that matches string. The strptime function doesn't work with timestamps that consist of only a month and year. The timestamps must include a day.

For example, if the string is 2022-08-22 17:19:01, the format must be %Y-%m-%d%H:%M:%S . The string date must be January 1, 1971 or later. The strptime function takes any date from January 1, 1971 or later, and calculates the UNIX time, in seconds, from January 1, 1970 to the date you provide.

The _time field is in UNIX time. In Splunk Web, the _time field appears in a human readable format in the UI but is stored in UNIX time. If you attempt to use the strptime function on the _time field, no action is performed on the values in the field.

Usage

You can use this function with the eval and where commands, in the WHERE clause of the from command, and as part of evaluation expressions with other commands.

With the strptime function, you must specify the time format of the string so that the function can convert the string time into the correct UNIX time. The following table shows some examples:

String timeMatching time format variables
Mon August 22 2022 17:19:01.89%a%B%d%Y%H:%M:%S.%N
Mon 8/22/2022 17:19:01.89%a%m/%d/%Y%H:%M:%S.%N
2022/08/22 17:19:01.89%Y/%m/%d%H:%M:%S.%N
2022-08-22T17:19:01.89%Y-%m-%dT%H:%M:%S.%N


For a complete list and descriptions of the format options you can use, see Using time variables in the SPL2 Search Manual.

Basic example

If the values in the timeStr field are hours and minutes, such as 11:59, the following example returns the time as a timestamp:

... | eval n=strptime(timeStr, "%H:%M")

Extended example

This example shows the results of using the strptime function.

Let's say you have a series of start and end times such as these:

starttimeendtime
Mon Aug 12 00:00:00 2019Mon Aug 12 05:59:59 2019
Mon Aug 12 06:00:00 2019Mon Aug 12 11:59:59 2019
Mon Aug 12 12:00:00 2019Mon Aug 12 17:59:59 2019
Mon Aug 12 18:00:00 2019Mon Aug 12 23:59:59 2019
Tue Aug 13 00:00:00 2019Tue Aug 13 05:59:59 2019
Tue Aug 13 06:00:00 2019Tue Aug 13 11:59:59 2019
Tue Aug 13 12:00:00 2019Tue Aug 13 17:59:59 2019
Tue Aug 13 18:00:00 2019Tue Aug 13 23:59:59 2019

When you run the following search, the eval command takes the string time values in the starttime field and returns the UNIX time that corresponds to the string time values.

...| eval startunix=strptime(starttime,"%a%B%d%H:%M:%S.%N%Y")

The results look something like this:

starttimeendtimestartunix
Mon Aug 23 00:00:00 2019Mon Aug 23 05:59:59 2019534143600.000000
Mon Aug 12 06:00:00 2019Mon Aug 12 11:59:59 20191534165200.000000
Mon Aug 12 12:00:00 2019Mon Aug 12 17:59:59 2019534186800.000000
Mon Aug 12 18:00:00 2019Mon Aug 12 23:59:59 20191534208400.000000
Tue Aug 13 00:00:00 2019Tue Aug 13 05:59:59 20191534230000.000000
Tue Aug 13 06:00:00 2019Tue Aug 13 11:59:59 20191534251600.000000
Tue Aug 13 12:00:00 2019Tue Aug 13 17:59:59 20191534273200.000000
Tue Aug 13 18:00:00 2019Tue Aug 13 23:59:59 20191534294800.000000

time()

This function returns the wall-clock time, in the UNIX time format, with microsecond resolution.

Usage

The value of the time() function will be different for each event, based on when the event is processed.

You can use this function with the eval and where commands, in the WHERE clause of the from command, and as part of evaluation expressions with other commands.

Basic example

This example shows the results of using the time() function.

Let's consider the following times:

starttimestarthuman
1534143600Mon Aug 13 00:00:00 2018
1534165200Mon Aug 13 06:00:00 2018
1534186800Mon Aug 13 12:00:00 2018
1534208400Mon Aug 13 18:00:00 2018
1534230000Tue Aug 14 00:00:00 2018
1534251600Tue Aug 14 06:00:00 2018
1534273200Tue Aug 14 12:00:00 2018
1534294800Tue Aug 14 18:00:00 2018

To return the UNIX time when a result is processed, you first need to convert the startime values to include microseconds.

... | eval epoch_time=strptime(starttime,"%s") | eval testtime=time()

  • The first eval command takes the numbers in the startime field and returns them with microseconds included.
  • The second eval command creates the testtime field and returns the UNIX time at the instant the result was processed by the eval command.


The results look something like this:

starttimestarthumanepoch_timetesttime
1534143600Mon Aug 13 00:00:00 20181534143600.0000001534376565.299298
1534165200Mon Aug 13 06:00:00 20181534165200.0000001534376565.299300
1534186800Mon Aug 13 12:00:00 20181534186800.0000001534376565.299302
1534208400Mon Aug 13 18:00:00 20181534208400.0000001534376565.299304
1534230000Tue Aug 14 00:00:00 20181534230000.0000001534376565.299305
1534251600Tue Aug 14 06:00:00 20181534251600.0000001534376565.299306
1534273200Tue Aug 14 12:00:00 20181534273200.0000001534376565.299308
1534294800Tue Aug 14 18:00:00 20181534294800.0000001534376565.299309

Notice the difference in the microseconds between the values in the epoch_time and test_time fields. You can see that the test_time values increase with each result.

See also

Function information
SPL2 eval functions Quick Reference
Overview of SPL2 eval functions
Naming function arguments in the SPL2 Search Manual
Date and Time functions - Splunk Documentation (2024)

FAQs

What format does Splunk use for date and time? ›

The values are stored in UNIX format and converted using the format specified, which is the ISO 8601 format. For example: 2022-04-13T14:00:15.000.

How to search date and time in Splunk? ›

To search for data using an exact date range, such as from October 15 at 8 PM to October 22 at 8 PM, use the timeformat %m/%d/%Y:%H:%M:%S and specify dates like earliest="10/15/2019:20:00:00" latest="10/22/2019:20:00:00" To search for data from the beginning of today (12 AM or midnight) use earliest=@d .

How does Splunk determine timestamp? ›

Splunk software adds timestamps to events at index time. It assigns timestamp values automatically by using information that it finds in the raw event data. If there is no explicit timestamp in an event, Splunk software attempts to assign a timestamp value through other means.

What is the difference between strftime and strptime in Splunk? ›

strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.

What formats for displaying date and time? ›

Overview of date and time formats

The time is displayed as, hh:mm:ss AM/PM, where hh is the hour, mm is minutes, and ss is seconds.

What is the default format for date and time? ›

By default, the date format is MM/DD/YYYY HH24:MI:SS.US.

How do you use date and time command? ›

Change with Command Prompt
  1. Press "Windows-R" to open the "Run" dialog, and type "cmd" into the box. ...
  2. Type "date" into the command prompt window and press "Enter." The current date setting will now display. ...
  3. Type "time" into the command prompt window and press "Enter." The current time setting will now display.

How do you find the date and time in a database? ›

The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format. Tip: Also look at the CURRENT_TIMESTAMP function.

How do I get the date from a timestamp field? ›

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.

How do you calculate timestamp time? ›

Discussion: If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.

How do you indicate a timestamp? ›

Timestamps are in the format [HH:MM:SS] where HH, MM, and SS are hours, minutes, and seconds from the beginning of the audio or video file.

How do you query a timestamp? ›

It returns the date in the YYYY-MM-DD format.
  1. For instance, SELECT CURRENT_DATE() returns the date I started writing this article: CURRENT_TIME(); ...
  2. CURRENT_TIMESTAMP(); The current timestamp function returns the current date and time. ...
  3. DATE. ...
  4. DATE_SUB. ...
  5. DATEDIFF. ...
  6. DAY. ...
  7. MONTH. ...
  8. YEAR.
Nov 15, 2022

What is the difference between POSIXct and POSIXlt and as date? ›

There are two POSIX date/time classes, which differ in the way that the values are stored internally. The POSIXct class stores date/time values as the number of seconds since January 1, 1970, while the POSIXlt class stores them as a list with elements for second, minute, hour, day, month, and year, among others.

What is difference between date and timestamp data types? ›

What is the difference between MySQL DATETIME and TIMESTAMP data type? Range − Datetime data type supports a date along with time in the range between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. But timestamp data type supports a date along with time in the range between '1970-01-01 00:00:01' to '2038-01-19 08:44:07'.

What is the difference between datetime and timestamp? ›

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.

How do you write the date and time in a document? ›

If you need to write both the time and date, just combine the rules. Here are two examples: My flight arrives on December 13 at 10:40 a.m. Your appointment is at 2 o'clock on February 27, 2022.
...
Here's what this looks like:
  1. I get out of school at 3:45 p.m.
  2. I get out of school at 3:45. ...
  3. I get out of school at 15:45.
May 31, 2022

Which attribute is used to display date or time content? ›

The datetime attribute of the <time> element is used to display the machine-readable date time.

What are the best practices for date formatting? ›

For date, always include four digit year and use numbers for months. For example, the date format yyyy-mm-dd would appear as 2011-03-15 (March 15, 2011). If Julian day is used, make sure the year field is also supplied. For example, mmm.

What is UTC date and time format? ›

Description. UTC() takes comma-delimited date and time parameters and returns the number of milliseconds between January 1, 1970, 00:00:00, universal time and the specified date and time. Years between 0 and 99 are converted to a year in the 20th century (1900 + year) . For example, 95 is converted to the year 1995 .

How to convert a String to datetime? ›

Converting a String to a datetime object using datetime.strptime() The datetime.strptime() method returns a datetime object that matches the date_string parsed by the format. Both arguments are required and must be strings.

What is T and Z in TIMESTAMP? ›

The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC).

Which is a date and time function? ›

Date and time functions are used when working with date, time, and duration data, and can perform operations like measuring the number of days (DAYS) between two dates, automatically inputting the current date (TODAY) or date and time (NOW), merging multiple values into one date value (DATE), and more.

Where do you find date and time settings? ›

On your Android phone, open Settings, then: Android 9 select System > Date and Time. Android 8 select General Management > Date and Time.

Where is the date and time stored? ›

Originally Answered: Where is time and date of a computer stored? The time is stored in a memory chip present on the motherboard (the main component of every computer). This chip is a simple flash memory (like any USB flash disk) which can keep data without being connected to electricity.

How do I convert timestamp to date and time? ›

Use the datetime. strptime() function(formats a time stamp in string format into a date-time object) to convert the timestamp to datetime object by passing the input timestamp and format as arguments to it. Print resultant datetime object.

How do you add 2 hours to a timestamp? ›

To add 2 hours in the current time, we will use the DATE_ADD() function. mysql> select DATE_ADD(now(),interval 2 hour);

How do you automate a timestamp? ›

To insert the current time into an Excel cell as a static timestamp, use one of the following keyboard shortcuts:
  1. To insert current time, press Ctrl + Shift + ;
  2. To enter current date and time, press Ctrl + ; which inserts a date, then press the Space key, and then hit Ctrl + Shift + ; to insert the current time.
Feb 3, 2023

How do you find the time difference between two dates? ›

Use the DATEDIF function when you want to calculate the difference between two dates. First put a start date in a cell, and an end date in another.
...
Calculate age in accumulated years, months, and days
  1. Use DATEDIF to find the total years. ...
  2. Use DATEDIF again with “ym” to find months. ...
  3. Use a different formula to find days.

What is the standard format for timestamp? ›

As of ISO 8601-1:2019, the basic format is T[hh][mm][ss] and the extended format is T[hh]:[mm]:[ss]. Earlier versions omitted the T (representing time) in both formats. [hh] refers to a zero-padded hour between 00 and 24. [mm] refers to a zero-padded minute between 00 and 59.

Is timestamp same as date? ›

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision.

Is timestamp a number or string? ›

A string representation of a timestamp is a character or a Unicode graphic string that starts with a digit and has a length of at least 14 characters.

How to write SQL query for timestamp? ›

The basic syntax of “timestamp” data type in SQL is as follows : Timestamp 'date_expression time_expression'; A valid timestamp data expression consists of a date and a time, followed by an optional BC or AD.

How to write date and time in SQL? ›

SQL Server comes with the following data types for storing a date or a date/time value in the database: DATE - format YYYY-MM-DD.
...
SQL Date Data Types
  1. DATE - format YYYY-MM-DD.
  2. DATETIME - format: YYYY-MM-DD HH:MI:SS.
  3. TIMESTAMP - format: YYYY-MM-DD HH:MI:SS.
  4. YEAR - format YYYY or YY.

How to pass date and time in SQL query? ›

For storing date and time, the different data types are:
  1. DATE – in YYYY-MM-DD format in SQL.
  2. YEAR – in YYYY or YY format in SQL.
  3. TIMESTAMP – in YYYY-MM-DD HH: MI:SS format in SQL.
  4. DATETIME – in YYYY-MM-DD HH: MI: SS format in SQL.

Is date and time a string? ›

A standard date and time format string uses a single character as the format specifier to define the text representation of a DateTime or a DateTimeOffset value. Any date and time format string that contains more than one character, including white space, is interpreted as a custom date and time format string.

What is the difference between datetime and TimeSpan? ›

The TimeSpan struct represents a duration of time, whereas DateTime represents a single point in time. Instances of TimeSpan can be expressed in seconds, minutes, hours, or days, and can be either negative or positive.

Which function is used to set dates into strings including time? ›

DateTimeValue function converts a date and time string (for example, "January 10, 2013 12:13 AM") to a date/time value.

Is date and time a metadata? ›

Under some metadata standards, time is a representation term used to specify a time of day in the ISO 8601 time format. Note that Time should not be confused with the DateAndTime representation term which requires that both the date and time to be supplied.

Is it better to use timestamp or datetime? ›

Timestamps are also lighter on the database and indexed faster. The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in YYYY-MM-DD HH:MM:SS format.

Which three are datetime data types? ›

Datetime and Interval Data Types. The datetime data types are DATE , TIMESTAMP , TIMESTAMP WITH TIME ZONE, and TIMESTAMP WITH LOCAL TIME ZONE .

What is timestamp function? ›

The TIMESTAMP function returns a timestamp from a value or a pair of values. TIMESTAMP ( expression1 ,expression2 ) The schema is SYSIBM. Only Unicode databases support an argument that is a graphic string representation of a date, a time, or a timestamp.

Why do we use datetime? ›

To Display the Current Date and Time

We use the function of datetime.

How do you define a datetime? ›

  1. The DateTime value type represents dates and times with values ranging from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) in the Gregorian calendar.
  2. Time values are measured in 100-nanosecond units called ticks.

What format does Splunk store data in? ›

Splunk stores data in a flat file format. All data in Splunk is stored in an index and in hot, warm, and cold buckets depending on the size and age of the data.

What is format in Splunk? ›

This command is used to format your sub search result. This command takes the results of a sub search and formats or combines the results into a single event and places that result into a new field called “search” as we have seen in case of “return” command.

How to change timestamp format in Splunk? ›

  1. use strptime() to parse a timestamp value.
  2. use strftime() to format a timestamp value.

How to change date format in Splunk? ›

Try this: ... | eval date=strftime(strptime(date,"%Y-%m-%d %H:%M:%S"), "%m-%d-%Y %H:%M:%S") | ...

How do I ingest data into Splunk? ›

You can add data inputs from the Splunk Web home page or by selecting Settings > Data Inputs.
  1. From the Splunk Web home page, click Add Data.
  2. Select Settings > Add data.
  3. Select Settings > Data inputs from the Data section of the Settings drop-down list.
Sep 21, 2022

Where are events stored in Splunk? ›

The events are stored in in the splunk indexers in indexes in a timestamp order. By default the retention size per index is 500GB and the time retention is 6 years. It can be changed of course depending of your needs and of your storage.

Does Splunk require a DB to store data? ›

A main benefit of Splunk is that it uses indexes to store data, and so does not require a separate database to store its information.

What are the 3 types of format commands that can be used to format data? ›

They let you select which properties you want to show. This article describes the Format-Wide , Format-List , and Format-Table cmdlets.

What are 3 main components in a Splunk architecture? ›

Splunk Components. The primary components in the Splunk architecture are the forwarder, the indexer, and the search head.

What are the 4 types of format commands that can be used to format data? ›

The 4 types of FORMAT commands includes:
  • Format-Wide.
  • Format-List.
  • Format-Table.
  • Format-Custom.

What is the default timestamp in Splunk? ›

A default field that represents time information in an event. Most events contain timestamps. In cases where an event does not contain timestamp information, Splunk Enterprise attempts to assign a timestamp value to the event at index time.

How do I change data from date to timestamp? ›

2. CAST. CAST() function performs the same way as CONVERT(), i.e. it converts any data type's value into the desired data type. Thus, we can use this function to convert the retrieved current timestamp into the date and time values.

What are the different timestamp formats? ›

With the fractional part included, the format for these values is ' YYYY-MM-DD hh:mm:ss [. fraction ]' , the range for DATETIME values is '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999' , and the range for TIMESTAMP values is '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999' .

How do you control date format? ›

Create a custom date format

Press Control+1 or Command+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.

What is the name of the time date field in Splunk? ›

strftime(time, format, time_zone)

How can I change the date format of a datetime? ›

To convert a datetime object into a string using the specified format, use datetime. strftime(format). The format codes are standard directives for specifying the format in which you want to represent datetime. The%d-%m-%Y%H:%M:%S codes, for example, convert dates to dd-mm-yyyy hh:mm:ss format.

Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 6243

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.