Needing to populate a title block or stamp a drawing with the current date/time is a very common task in the AutoCAD product along with determining the time it might have taken to execute a custom program for return of investment (ROI) analysis. AutoCAD-based products support several system variables that can be used to determine the current date/time, these system variables are:
- CDATE - Stores the current date and time as a decimal value.
- DATE - Stores the current date and time in Modified Julian Date format.
- MILLISECS - Stores the current number of milliseconds since the computer was booted.
NOTE: Starting with AutoCAD 2017, the
DATE system variables no longer store the current time down to milliseconds.
CDATE System Variable
CDATE system variable is the most common approach to getting the current date and time within the AutoCAD environment while using AutoLISP. When queried, the
CDATE system variable returns a value as a real number. The
GETVAR function is used to return the value of a system variable for use with the AutoLISP programming language.
Here is an example of getting the value stored in the
CDATE system variable:
Command: (getvar "CDATE")
As previously mentioned, the value of the
CDATE system variable is a real number. By default, it isn't possible to see the full value of the
CDATE system variable without a bit of extra help. It is best to convert the real number to a string, so you can see and work with the full value which can be accomplished using the
RTOS function. The
RTOS function accepts 3 arguments, the real number to convert to a string and two optional arguments which represent the linear unit mode and precision in which the value should be formatted.
Here is an example of converting the value stored in the
CDATE system variable to a string that represents the real value as a decimal with a precision of 6:
Command: (rtos (getvar "CDATE") 2 6)
NOTE: Prior to AutoCAD 2017-based products, using a precision of 8 would include milliseconds. Starting with AutoCAD 2017-based products, the last two places that represent milliseconds are returned as "00".
The string returned by the
RTOS function in the previous example allows you to see the full real number. The whole number prior to the decimal represents the current date while the fraction represents the current time.
Formatting the CDATE Value with AutoLISP
This code sample demonstrates how to pull the string apart that is returned by the
RTOS function into the current Month, Day, Year, Hours, Minutes, and Seconds:
; Prints or outputs the current date and time
; bOutput = T - Output the string to the Command prompt
; bOutput = nil - Return a formatted string MM/DD/YYYY HH:MM:SS
; Usage: (CurDate T); Usage: (CurDate nil)
(defun CurDate (bOuput / cdate_val YYYY M D HH MM SS)
; Get the current date/time
(setq cdate_val (rtos (getvar "CDATE") 2 6))
; Break up the string into its separate parts
(setq YYYY (substr cdate_val 1 4)
M (substr cdate_val 5 2)
D (substr cdate_val 7 2)
HH (substr cdate_val 10 2)
MM (substr cdate_val 12 2)
SS (substr cdate_val 14 2)
; Output the current date and time to the Command
; prompt or return the formatted output as a string
(prompt (strcat "\nDate: " M "/" D "/" YYYY
"\nTime: " HH ":" MM ":" SS
(strcat M "/" D "/" YYYY " " HH ":" MM ":" SS)
The output that is generated from the
CurDate function will look similar to the following:
Hope this article gives you a better understanding of how the value of the
CDATE system variable can be used to get and format the current date with AutoLISP.
Other related articles:
- Getting the Current Date/Time with AutoLISP - Part 2
- PSA: Do Your AutoLISP Programs Use the CDATE or DATE System Variables?
- PSA: Additional Date Related System Variables to Be Aware Of in Custom AutoLISP Programs