Current Date
To get the current date, you can use the “Sys.Date()” function.
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()
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()
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.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.