Format Date

tvman5683

Board Regular
Joined
Mar 23, 2009
Messages
94
I'm using a line of code to identify a file dated yesterday. This morning I noticed a result I didn't expect.
The result I saw was a date of "20160200". I've used the minus 1 before and never saw that. Can anyone give me any ideas??

Thanks
John



HTML:
Format(Date, "yyyymmdd") - 1
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
The problem is that the FORMAT function does not return a Date value, but rather a Text (string) value. So you cannot do date math like that on it like that. Subtracting 1 is coerceing it from Text to Number, but it is viewing at as the integer 20160201 (not a date, because dates aren't stored like that).

If you want to due date math like that, do it BEFORE applying the FORMAT functions, i.e.
Code:
Format(Date - 1, "yyyymmdd")
 
Last edited:
Upvote 0
You are trying to subtract 1 from the formatted result (within the format function) when you should be subtracting it from Date:
Format(Date-1, yyyymmdd)
 
Upvote 0
Format(Date-1, yyyymmdd)
Don't forget the double quotes around "yyyymmdd" like I showed in my response above!
;)
 
Upvote 0
Crap! Not only did I post while you were posting, I forgot the quotes.
 
Upvote 0
Not only did I post while you were posting
You mean I found some one who types even slower than I do?!?!?:LOL:
Usually I am on the other end (that half-semester of typing in Junior High really didn't help me that much!)
 
Upvote 0

Forum statistics

Threads
1,221,828
Messages
6,162,213
Members
451,752
Latest member
freddocp

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top