How to Get Current Date and Time in R

Current Date

To get the current date, you can use the “Sys.Date()” function.

Get Current Date and Time in R

Sys.Date()

Output

[1] "2023-05-23"

Current Date and Time

To get the current date and time, use the “Sys.time()” function.

Sys.time() function

Sys.time()

Output

[1] "2023-05-23 22:13:24 IST"

To get the system time in a fixed-format character string, use the “date()” function.

date() function

date()

Output

[1] "Thu Mar 11 16:59:16 2021"

Formatting Date and Time

If you need to format the date and time in a specific way, you can use the format() function:

formatted_datetime <- format(Sys.time(), "%Y-%m-%d %H:%M:%S")

print(formatted_datetime)

Output

[1] "2023-12-24 18:47:06"

Time Zones

If you need to work with a specific time zone, you can use the “tz” argument in the format() function:

formatted_datetime_tz <- format(Sys.time(), "%Y-%m-%d %H:%M:%S", tz = "GMT")

print(formatted_datetime_tz)

Output

[1] "2023-12-24 18:47:06"

This will format the current date and time in the GMT time zone.

Leave a Comment