Changing a String for Certain Dates
Hi all,
I am new to VB6
My question is, I have a date in the from of MDDYYYY... I need it to be in the from o f YYMMDD.
My program works for months that have two integers (10, 11, and 12)...but does not work for months with just one integer (1, 2, 3, 4, 5, 6, 7, 8, 9) and the reason for this, is because I need to append a 0 to the beginning for those months...
How do I add a 0 to the months that are represented with one integer and not add it to the months that are represented with two integers?
Thanks in advance
Code:
thedate = MonthView1.Value ' Grabs date from the calender (in the form mddyyyy)
month = Mid(thedate, 1, 1) ' define the month
day = Mid(thedate, 2, 2) ' define the day
year = Mid(thedate, 4, 4) ' define the year
mod_year = Mid(year, 3, 2) ' shorten the year from yyyy to yy
mod_date= mod_year & month & day
Re: Changing a String for Certain Dates
Well, first, you got a lot of things wrong....month, day and year are actually VB6 functions...don't use them as variables.
You really shouldn't work with dates like this...it would behoove you to look up the format function.
HOWEVER, if you insist on using it in this manner (and you change your string variables to say, myMonth, myDay, Myyear),
then you COULD do this:
if len(trim(myMonth)) = 1 then
myMonth = "0" & myMonth
end if
Re: Changing a String for Certain Dates
Code:
mod_date = Right$(Year(MonthView1), 2&) & Right$("0" & Month(MonthView1), 2&) & Right$("0" & Day(MonthView1), 2&)
Re: Changing a String for Certain Dates
Thanks Sam,
That makes sense...But wouldn't that affect month numbers 10, 11, and 12 because they start with 1 as well?
Re: Changing a String for Certain Dates
The format funciton is really the way to go for you
Code:
MsgBox Format(MonthView1.Value, "YYMMDD")
Re: Changing a String for Certain Dates
No, Tech....that function I did (len), is looking at the LENGTH of that month string. if it is only 1 character long, it would append the "0", otherwise it would not do anything.
Re: Changing a String for Certain Dates
I agree with MarkT You should use the Format$() function. This is what it is for. You can of course use other methods and get it to work but Format$() is the correct choice.
Re: Changing a String for Certain Dates
As for breaking it down you should not be using Variables named Month Day and Year as these are names of Vb functions.
To get the year from a date you would call the Year() function, to get the month you would call the Month() function and to get the day you would call the Day() function
Re: Changing a String for Certain Dates
Mark's got the right idea there ... just don't let the format get bed in the first place... if MonthView1.Value returns a date value in the first place... jsut format that right from the get-go... no need to muss around with other formats or parsing things out.
-tg
Re: Changing a String for Certain Dates
Question...FOR Terh...but answered by experts.....MarkT says the same as I would have:
my2DigYrDate = Format(MonthView1.Value, "YYMMDD")
but, when now looking for month, day and year, using the same vb functions (month, day, year).
I'm getting bogus results.
For example if monthview1.value = #1/14/2013# then my2DigYrDate would be #130114#.
But, now I want to get the day, month and year.
So I did myDay = day(my2DigYrDate) and got "27" instead of "14"
and I did myMonth = month(my2DigYrDate) and got "3" instead of "01"
and I did myYear = year(my2DigYrDate) and got "2256" instead of "13"
What did "I" miss/mess up? (Yes, I know...look up FORMAT (as I even suggested)....but it gets me every once in awhile).
Re: Changing a String for Certain Dates
Format/Format$ returns a Variant/String, not Date.
Re: Changing a String for Certain Dates
okay, but if I do this: myMonth = Month(CDate(my2DigYrDate)) (etc), I still get the same 27, 3, 2256 what am I missing?
It appears cdate is changing the variable back into DDMMYYYY????? or something.
Re: Changing a String for Certain Dates
Quote:
Originally Posted by
SamOscarBrown
Question...FOR Terh...but answered by experts.....MarkT says the same as I would have:
my2DigYrDate = Format(MonthView1.Value, "YYMMDD")
but, when now looking for month, day and year, using the same vb functions (month, day, year).
I'm getting bogus results.
For example if monthview1.value = #1/14/2013# then my2DigYrDate would be #130114#.
But, now I want to get the day, month and year.
So I did myDay = day(my2DigYrDate) and got "27" instead of "14"
and I did myMonth = month(my2DigYrDate) and got "3" instead of "01"
and I did myYear = year(my2DigYrDate) and got "2256" instead of "13"
What did "I" miss/mess up? (Yes, I know...look up FORMAT (as I even suggested)....but it gets me every once in awhile).
It is because of the way you formatted the date into the string.
You would need to format it in the same fashion as the system date for it to work properly from a string.
Consider this for example
Code:
Private Sub Command1_Click()
Dim Date1 As String
Dim date2 As String
Date1 = "130114"
date2 = "1/14/13"
Debug.Print Month(Date1)
Debug.Print Month(date2)
End Sub
one will give the correct result and the other will not
Re: Changing a String for Certain Dates
Yup, DM....I see that....but, now back to the OP. He/she wanted a 2-character month...when I use the following, my month (for January), returns 1, not 01. So, OP still needs a way to make the month 2 characters (as I had suggested with a concatenation if statement)...But is all that necessary, can't that also be returned with the format function? AND, OP wants a TWO digit year. See my example below:
Code:
my2DigYrDate = Format(MonthView1.Value, "YY/MM/DD")
'''EDIT: 'my2DigYrDate is "13/01/14"
myMonth = Month(CDate(my2DigYrDate))
myday = Day(CDate(my2DigYrDate))
myYear = Year(CDate(my2DigYrDate))
MsgBox myMonth 'returns '1' not, '01'
MsgBox myday 'returns '14' correctly
MsgBox myYear 'returns '2012' 4 digits
Re: Changing a String for Certain Dates
to format a number to 2 places you can use the format function
Code:
theMonth=format$(Month(Date),"0#")
Edit: My main purpose in mentioning those functions was 2 fold
1: To show why the OP should not use the variable names he/she was using
2: To point out that these are available if needed.
Re: Changing a String for Certain Dates
Year(CDate(my2DigYrDate))
for crying out loud WHYYYYY?
OK... look, here's the bottom line... if you want something to be a date, treat it as a date and keep it as a date... don't be changing it to a string and then expecting to be able to get back to where you were... it just isn't going to happen. the month(), year() and day() functions should only be used against dates... using the CDate function doesn't help because yymmdd IS NOT A RECOGNIZED date format... it's just a number... VB will convert it the best it can. as you're finding out, it gets it wrong more than it gets it right. If you're going to be needing the parts of a date START with a DATE, WORK with the DATE, and LEAVE it as a date. I suspect that what you're doing is confusing the value with the display/format... and the two are not the same... because the format loses context... only the VALUE of the date actually has any meaning... consider this: 3/4/13 ... what is that? Mar 14, 2013? Or is it Apr 3, 2013? You don't know do you? Because in the formatting of the display we lost context and subsequently the value of the date. It sounds like you're wanting to force a format, then assume that that format has any value or meaning... except all you have is a string that holds some number: 130403 ... there's no context, so there's no way to know that it means Apr 3, 2013... because for all I know it's Apr 13, 2004 ... you can get the date parts all you want, but you need to get them FROM THE DATE ITSELF... once you have those parts, you can do with them what you want... even reassemble them into a number... but that's all it is ... a number... don't try to do anything date-centric on that number. If you need both the number and the date... then STORE the DATE as a DATE somewhere... don't try to shoe-horn the number into something it isn't. Because one thing it certainly isn't... is a date.
-tg
Re: Changing a String for Certain Dates
Gee Tech.....just tryin' to learn, that's all. :-(
Re: Changing a String for Certain Dates
It's not just you... it's the whole thread.. it's something I see time and time again... people misusing or misunderstanding or simply not getting it... date are not strings and strings shouldn't be used as dates...
-tg