Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Wierd Bug with Format Date VBA???

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?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Wierd Bug with Format Date VBA???

Hi,

The functions Month(), Day(), Format() (and Year()) expect a date but not
as a text string but in Excels internal date format.

To solve it you can use another variable to record date or use the now
function or extract the details from the string:

dim tempDate as Date ' new variable
tempDate = Now ' set it to date
theDate = Format(Now,"mm-dd-yy") ' as before
theDate = Format(tempDate,"mm-dd-yy") 'using temp variable
themonth = Month(Now) ' as before with Now
themonth = Month(tempDate) ' using temp variable
themonth = format(Now,"mmmm") ' with Now giving the full month
themonth = left(theDate,2) ' extracting from the string
theDay = Day(Now) ' as before with Now
theDay = Day(tempDate) 'using temp variable
theDay = format(Now, "d") ' as before with Now
theDay = mid(theDate, 4,2) ' extracting from the string
wkday = Format(Now, "ddd") ' as before with Now
wkday = Format(tempDate, "ddd") 'using temp variable

--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


" wrote:

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?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Wierd Bug with Format Date VBA???

I'm not quite sure I understand what you're doing, but

wkday = format(11,"ddd")
isn't formatting the 11th of this month. It's formatting the 11th day after day
0. (Day 0 in VBA is 12/30/1899).

If you try:
(with theday = 11)
MsgBox Format(theday, "mm/dd/yyyy")
You'll see 01/10/1900

Option Explicit
Sub stuff()
Dim TheDate As Date
Dim TheMonth As Long
Dim TheDay As Long
Dim TheYear As Long
Dim WkDay As String

TheDate = Date
'not sure why you want the next two lines
'TheMonth = Month(TheDate)
'TheDay = Day(TheDate)
WkDay = Format(TheDate, "ddd")
MsgBox WkDay
End Sub




wrote:

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?


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Wierd Bug with Format Date VBA???

Ps. WeekdayName was added in xl2k (or xl2002???):

MsgBox WeekdayName(Weekday(Date), abbreviate:=True)



Dave Peterson wrote:

I'm not quite sure I understand what you're doing, but

wkday = format(11,"ddd")
isn't formatting the 11th of this month. It's formatting the 11th day after day
0. (Day 0 in VBA is 12/30/1899).

If you try:
(with theday = 11)
MsgBox Format(theday, "mm/dd/yyyy")
You'll see 01/10/1900

Option Explicit
Sub stuff()
Dim TheDate As Date
Dim TheMonth As Long
Dim TheDay As Long
Dim TheYear As Long
Dim WkDay As String

TheDate = Date
'not sure why you want the next two lines
'TheMonth = Month(TheDate)
'TheDay = Day(TheDate)
WkDay = Format(TheDate, "ddd")
MsgBox WkDay
End Sub

wrote:

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?


--

Dave Peterson


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Wierd Bug with Format Date VBA???

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?





  #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?





Reply
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 10:41 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"