How is the TODAY() function able to format the cell as a Date?

%3CLINGO-SUB%20id%3D%22lingo-sub-2040682%22%20slang%3D%22en-US%22%3EHow%20is%20the%20TODAY()%20function%20able%20to%20format%20the%20cell%20as%20a%20Date%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2040682%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20an%20XLL%20which%20I%20have%20coded%20up%2C%20and%20some%20of%20the%20functions%20defined%20therein%20return%20dates%2C%20that%20is%20they%20return%20doubles%20which%20represent%20serial%20dates.%20They%20appear%20in%20the%20spreadsheet%20in%20General%20format%20(eg%2043298).%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20I%20enter%20the%20formula%20%3DTODAY()%20in%20a%20cell%20that%20was%20previously%20empty%2C%20I%20get%20today's%20date%20displayed%20and%20the%20cell's%20format%20has%20been%20changed%20to%20Date.%20That%20is%20also%20true%20if%20I%20enter%20%3DTODAY()%20%2B%201%2C%20but%20not%20if%20I%20enter%20%3D2%20*%20(TODAY()%2F2).%3CBR%20%2F%3E%3CBR%20%2F%3ESo%2C%20how%20does%20Excel%20decide%20which%20format%20to%20apply%3F%20Can%20I%20somehow%20tap%20into%20this%20decision-making%20to%20get%20the%20doubles%20from%20my%20XLL%20UDF%20displayed%20as%20dates%3F%20Typically%20UDFs%20are%20not%20allowed%20to%20change%20the%20format%20of%20the%20calling%20cell.%3CBR%20%2F%3E%3CBR%20%2F%3E(cf%20UDFs%20written%20in%20VBA%2C%20where%20you%20use%20a%20Date%20type%2C%20or%20return%20a%20Variant%20of%20type%20VT_DATE).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2040682%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2041141%22%20slang%3D%22de-DE%22%3ESubject%3A%20How%20is%20the%20TODAY()%20function%20able%20to%20format%20the%20cell%20as%20a%20date%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2041141%22%20slang%3D%22de-DE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F921344%22%20target%3D%22_blank%22%3E%40DS_London%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3EHere%20is%20a%20workbook%20with%20a%20list%20of%20options%20to%20help%20you%20decide.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3EIf%20anything%20more%20specific%20is%20required%20please%20let%20me%20know.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3EIf%20the%20post%20helped%20you%2C%20please%20mark%20it%20as%20the%20%22correct%20answer%22%20so%20that%20others%20in%20this%20forum%20can%20benefit%20from%20it%20too.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3EThanks%2C%20I%20hope%20I%20could%20help.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3ENiolino%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3EI%20know%20I%20don't%20know%20(Socrates)%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2043208%22%20slang%3D%22en-US%22%3EBetreff%3A%20How%20is%20the%20TODAY()%20function%20able%20to%20format%20the%20cell%20as%20a%20Date%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2043208%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F722750%22%20target%3D%22_blank%22%3E%40Nikolino%3C%2FA%3E%26nbsp%3Bfor%20the%20suggestion%2C%20but%20Data%20Validation%20doesn't%20address%20the%20question%20(that%20is%20a%20user-defined%20layer%20on%20top%20of%20the%20basic%20worksheet).%3CBR%20%2F%3E%3CBR%20%2F%3ETry%20this%3A%20go%20to%20a%20blank%20cell%20in%20a%20new%20Workbook%20(which%20usually%20has%20the%20%22General%22%20number%20format).%20Enter%20the%20function%20%3DTODAY()%20...%20what%20happens%3F%3CBR%20%2F%3E%3CBR%20%2F%3EYou%20get%20today's%20date%20as%20a%20readable%20date%2C%20and%20the%20cell%20number%20format%20has%20been%20changed%20to%20%22Date%22%20(using%20the%20region-specific%20short%20date%20format).%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20TODAY()%20function%20returns%20a%20double%20(the%20type%20with%20which%20Excel%20stores%20all%20numbers%2C%20and%20how%20Excel%20understands%20dates%20...%20days%20since%201-Jan-1900).%20Clearing%20the%20format%20(Ctrl-Shift-%23)%20shows%20that%20number%20(eg%2044024).%3CBR%20%2F%3E%3CBR%20%2F%3EWhy%20(and%20how)%20does%20Excel%20decide%20that%20the%20double%20returned%20from%20TODAY()%20should%20trigger%20additional%20processing%20and%20formatting%20of%20the%20target%20cell%3F%20Why%20does%20%3DTODAY()%20%2B%201%20also%20trigger%20this%20reformatting%2C%20but%20%3D2%20*%20(TODAY()%2F2.0)%20not%3F%20Note%20this%20only%20happens%20when%20the%20formula%20is%20first%20entered%3A%20not%20if%20the%20function%20is%20pasted%20in%20(Paste%20Special%20%2F%20Functions)%2C%20or%20if%20the%20user%20subsequently%20changes%20the%20format%20of%20the%20cell%20manually.%20So%20perhaps%20it%20is%20part%20of%20Excel's%20processing%20and%20parsing%20of%20the%20function%20text%20entry%2C%20which%20has%20special%20cases%20for%20some%20functions%20and%20not%20others.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3DNOW()%20also%20applies%20additional%20formatting%20to%20the%20cell%20(%22Custom%22).%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20you%20define%20a%20UDF%20in%20VBA%20as%20returning%20a%20Date%20type%20(or%20even%20a%20Variant%20of%20type%20Variant%2FDate)%2C%20it%20still%20appears%20in%20the%20worksheet%20as%20a%20number%2C%20and%20the%20cell's%20number%20format%20is%20unchanged.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EI%20would%20like%20my%20C%2B%2B%20XLL-defined%20functions%20to%20get%20a%20piece%20of%20this%20action%20for%20formatting%20cells%20for%20Dates%2C%20like%20TODAY%20and%20NOW%20do.%20Is%20it%20possible%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E(NB.%20I%20am%20using%20the%20C-style%20Excel%20SDK%20for%20my%20XLL%2C%20not%20the%20C%23%20route%2C%20so%20workarounds%20that%20plug%20into%20the%20Excel%20object%20model%2C%20while%20not%20impossible%2C%20would%20be%20somewhat%20excessive).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I have an XLL which I have coded up, and some of the functions defined therein return dates, that is they return doubles which represent serial dates. They appear in the spreadsheet in General format (eg 43298).

If I enter the formula =TODAY() in a cell that was previously empty, I get today's date displayed and the cell's format has been changed to Date. That is also true if I enter =TODAY() + 1, but not if I enter =2 * (TODAY()/2).

So, how does Excel decide which format to apply? Can I somehow tap into this decision-making to get the doubles from my XLL UDF displayed as dates? Typically UDFs are not allowed to change the format of the calling cell.

(cf UDFs written in VBA, where you use a Date type, or return a Variant of type VT_DATE).

2 Replies

@DS_London 

 

Here is a workbook with a list of options to help you decide.

 

If anything more specific is required please let me know.

If the post helped you, please mark it as the "correct answer" so that others in this forum can benefit from it too.

 

Thanks, I hope I could help.

 

Niolino

I know I don't know (Socrates)

Thanks @Nikolino for the suggestion, but Data Validation doesn't address the question (that is a user-defined layer on top of the basic worksheet).

Try this: go to a blank cell in a new Workbook (which usually has the "General" number format). Enter the function =TODAY() ... what happens?

You get today's date as a readable date, and the cell number format has been changed to "Date" (using the region-specific short date format).

The TODAY() function returns a double (the type with which Excel stores all numbers, and how Excel understands dates ... days since 1-Jan-1900). Clearing the format (Ctrl-Shift-#) shows that number (eg 44024).

Why (and how) does Excel decide that the double returned from TODAY() should trigger additional processing and formatting of the target cell? Why does =TODAY() + 1 also trigger this reformatting, but =2 * (TODAY()/2.0) not? Note this only happens when the formula is first entered: not if the function is pasted in (Paste Special / Functions), or if the user subsequently changes the format of the cell manually. So perhaps it is part of Excel's processing and parsing of the function text entry, which has special cases for some functions and not others.

 

=NOW() also applies additional formatting to the cell ("Custom").

If you define a UDF in VBA as returning a Date type (or even a Variant of type Variant/Date), it still appears in the worksheet as a number, and the cell's number format is unchanged.

I would like my C++ XLL-defined functions to get a piece of this action for formatting cells for Dates, like TODAY and NOW do. Is it possible?

 

(NB. I am using the C-style Excel SDK for my XLL, not the C# route, so workarounds that plug into the Excel object model, while not impossible, would be somewhat excessive).