LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Wierd Bug with Format Date VBA???

Oops!

Day 11 in Excel is Wednesday, January 11, 1900.
Day 11 in VBA is Wednesday, January 10, 1900.

The difference is that VBA knows 1900 was not a leap year. Microsoft knew it
was a leap year when they created Excel, but for compatibility with Lotus
1-2-3, which didn't know from leap years (nor from business models
apparently), they pretended it was a leap year. But I got the Wednesday part
right.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"Jon Peltier" wrote in message
...
theDay is the day of the month, a simple numerical value, 11 (for 11 Jan
2007). Format(theDay, "ddd") accepts this number 11, and tells you "Wed".
But 11 is not today's date, the date corresponding to 11 is Wednesday,
January 11, 1900. The bug is in the way you used the functions one after
the other. If you instead use this line:

wkday2 = Format(theDate, "ddd")

you get the expected result of "Thu". But you should keep dates in date
variables, not in strings, because you never know how VBA might
misinterpret the text. Here's how you should rewrite your code, including
declaring your variables as the types you expect them to be, not what VBA
arbitrarily assigns:

Sub DateStuff()
Dim theDate As Date
Dim theDateString As String
Dim theMonth As Integer
Dim theDay As Integer
Dim wkday As String

theDate = Now
theDateString = Format(theDate, "mm-dd-yy")
theMonth = Month(theDate)
theDay = Day(theDate)
wkday = Format(theDate, "ddd")
End Sub

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


wrote in message
ps.com...
sub stuff()
theDate = Format(Now,"mm-dd-yy")
themonth = Month(theDate)
theDay = Day(theDate)
wkday = Format(theDay, "ddd")
end sub

.... for some reason when i use the Format() function, it takes the day
before. Is there an option statement that I am missing or something?
The first format string returns the correct day, but the second one
returns the previous day. Am i missing an option statement?





 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
permanent conversion of 1904 date format to 1900 date format Jos Excel Worksheet Functions 4 November 26th 15 02:48 PM
How do I convert dd/mm/yy date format to yyddd Julian date format itzy bitzy[_2_] Excel Worksheet Functions 8 December 11th 09 03:20 AM
Convert date + time text format to date format Paul Ho Excel Worksheet Functions 2 May 22nd 07 05:47 PM
code to convert date from TEXT format (03-02) to DATE format (200203) Gauthier[_2_] Excel Programming 0 September 22nd 04 03:26 PM
Change a date in text format xx.xx.20xx to a recognised date format concatenator Excel Programming 1 November 24th 03 11:33 PM


All times are GMT +1. The time now is 06:42 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"