Solved

# using a split function

Posted on 2008-10-17
Medium Priority
252 Views
Hello Experts,
I dont know If I am doing something wrong here, please can you help.

All I wanted to do is to split the date by day/month/year and assign the values
back to corresponding dropdownlist controls..

regards
kay
``````RegDetails.DateOfBirth="10/02/1978"

If RegDetails.DateOfBirth <> "" Then
Dim DateBirth, DayBirth, MonthBirth, YearBirth As String

DateBirth = RegDetails.DateOfBirth.Split("/").ToString

DayBirth = DateBirth(0).ToString
MonthBirth = DateBirth(1).ToString
YearBirth = DateBirth(2).ToString

lstDayOfYourBirth.Text = DayBirth
lstMonthOfYourBirth.Text = MonthBirth
lstYearOfYourBirth.Text = YearBirth
End If
``````
0
Question by:learningnet
• 6
• 3
• 2
• +3

LVL 25

Accepted Solution

SStory earned 400 total points
ID: 22740220
I wonder if your DataOfBirth is declared as a string or DateTime?
I am thinking if it is a datetime, then you might need to do:
RegDetails.DateOfBirth.ToString.Split("/")

Also this returns an array of strings not a string

dim Parts() as string=RegDetails.DateOfBirth.ToString.Split("/")

should work
0

LVL 71

Assisted Solution

Éric Moreau earned 400 total points
ID: 22740233
Try this:

Dim DateBirth() as string
dim DayBirth, MonthBirth, YearBirth As String

DateBirth = RegDetails.DateOfBirth.Split("/")

DayBirth = DateBirth(0)
MonthBirth = DateBirth(1)
YearBirth = DateBirth(2)
0

LVL 61

Assisted Solution

Kevin Cross earned 400 total points
ID: 22740253
Whether String or DateTime, it may be easier just to convert to DateTime and use the built in functionality to get Month, Day, Year, Minute, Seconds, etc. all separate without having to depend on splitting by the "/".  This will allow your code to work even if someone enters date as "20081017" OR "10-17-2008" without you having to add extra code.
0

LVL 21

Assisted Solution

silemone earned 400 total points
ID: 22740268
well i suggest instead of using split, change date to

Dim objDT as DateTime  = Convert.ToDateTime(RegDetails.DateOfBirth)
DayBirth = objDT.Day
MonthBirth = objDT.Month
YearBirth = objDT.Year

objDT.Day returns an int, so if you need it as a string, then DayBirth = objDT.Day + ""
0

LVL 21

Expert Comment

ID: 22740294
just to make clear;  all objDT.Day, objDT.Month, and objDT.Year returns ints so if you're using string just at +""
or toString()
0

LVL 61

Expert Comment

ID: 22740312
Best practice with dates from Microsoft (interesting reading):
http://msdn.microsoft.com/en-us/library/ms973825.aspx#datetime_topic7

DateTime class members:
http://msdn.microsoft.com/en-us/library/system.datetime_members.aspx

You can use the Month() and other functions, but as you will see above you can just call named members of the DateTime structure to get same results.

Regards,
Kevin
0

LVL 21

Expert Comment

ID: 22740319
RegDetails.DateOfBirth="10/02/1978"

If RegDetails.DateOfBirth <> "" Then

Dim objDT as DateTiem = Convert.ToDateTime(RegDetails.DateOfBirth)

lstDayOfYourBirth.Text = objDT.Day.ToString()
lstMonthOfYourBirth.Text = objDT.Month.ToString()
lstYearOfYourBirth.Text = objDT.Year.ToString()
End If
0

LVL 6

Expert Comment

ID: 22740321
Cast the value as a DateTime and then use Day, Month and Year properties.
0

LVL 21

Expert Comment

ID: 22740336
Fixed typo..

RegDetails.DateOfBirth="10/02/1978"

If RegDetails.DateOfBirth <> "" Then

Dim objDT as DateTime = Convert.ToDateTime(RegDetails.DateOfBirth)

lstDayOfYourBirth.Text = objDT.Day.ToString()
lstMonthOfYourBirth.Text = objDT.Month.ToString()
lstYearOfYourBirth.Text = objDT.Year.ToString()
End If
0

LVL 21

Expert Comment

ID: 22740373
if you want month and day to be preceeded by 0,   i.e.  if 1,  then 01

RegDetails.DateOfBirth="10/02/1978"

If RegDetails.DateOfBirth <> "" Then
Dim objDT as DateTime = Convert.ToDateTime(RegDetails.DateOfBirth)
if  objDT.Day < 10 Then
lstDayOfYourBirth.Text = "0" + objDT.Day.ToString()
else
lstDayOfYourBirth.Text = objDT.Day.ToString()
End If
if  objDT.Day < 10 Then
lstMonthOfYourBirth.Text = "0" + objDT.Month.ToString()
else
lstMonthOfYourBirth.Text = objDT.Month.ToString()
End If
lstYearOfYourBirth.Text = objDT.Year.ToString()
End If

RegDetails.DateOfBirth="10/02/1978"

If RegDetails.DateOfBirth <> "" Then

Dim objDT as DateTime = Convert.ToDateTime(RegDetails.DateOfBirth)

lstDayOfYourBirth.Text = objDT.Day.ToString()
lstMonthOfYourBirth.Text = objDT.Month.ToString()
lstYearOfYourBirth.Text = objDT.Year.ToString()
End If

0

LVL 61

Expert Comment

ID: 22740399
You can also use the:
0

Author Comment

ID: 22741274
Thanks million for the input folks..

Let me try to put in a precise way , I have the following on the page

<asp:DropDownList ID="lstDayOfYourBirth" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="lstMonthOfYourBirth" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="lstYearOfYourBirth" runat="server">
</asp:DropDownList>
And I am filling them using the following for the first time the user visits the page

WebFunctions.FillDropDownListWithDate(lstYearOfYourBirth, 1890, Year(Now))
WebFunctions.FillDropDownListWithMonth(lstMonthOfYourBirth)
WebFunctions.FillDropDownListWithDate(lstDayOfYourBirth, 1, 31)

And using this....

Public Shared Sub FillDropDownListWithDate(ByVal lst As DropDownList, ByVal first As Integer, ByVal last As Integer)
Dim i As Integer

'lst.Items.Clear()
For i = first To last
Next
End Sub

Public Shared Sub FillDropDownListWithMonth(ByVal lst As DropDownList)
For i As Integer = 1 To 12
Next
End Sub

This all works fine ...

however, when they come to page to view and change details I wanted to load the dropdown list with the values they
chosesn the first time

Dim DateBirth() As String
Dim DayBirth, MonthBirth, YearBirth As String

DateBirth = RegDetails.DateOfBirth.Split("/")

DayBirth = DateBirth(0)
MonthBirth = DateBirth(1)
YearBirth = DateBirth(2)

lstDayOfYourBirth.Text = DayBirth
lstMonthOfYourBirth.Text = MonthBirth
lstYearOfYourBirth.Text = YearBirth

emoreau', suggested code seems to have pulled the data correct , however, the FillDropDownListWithDate is over writing it with "-"s

I hope you see what I am trying to do here?

I am also dealing with initial zeroer in the beginning

Public Shared Function FormatDateWithZeros(ByVal d As Date) As String
Return Right("0" & d.Day, 2) & "/" & Right("0" & d.Month, 2) & "/" & d.Year
End Function

To sum all, when I wanted to assing newly added value back to the drop down I would rather do not display the initial "0"?

Thanks again for all your help folks..

0

LVL 6

Assisted Solution

openshac earned 400 total points
ID: 22741460
Use string.Format("{0:dd/MM/yyyy}", myDate) instead of the function FormatDateWithZeros.

To get the number without a zero just use string.Format("{0:#}"), myNumberwithaZero)
0

LVL 21

Expert Comment

ID: 22742352
have you tested addin my code in...one code I offered places 0's in front, the other will not...
But it does use DateTime...way less code...I read your post and still not understanding why you can't use DateTime unless specs say you must split...
0

LVL 25

Expert Comment

ID: 22744980
seems to me you would convert it to a datetime, then use the
.Month
.Day
.Year
properties of that variable to set them in your combo boxes.

Example... a dropdown is indexed by 0 so

dim dtVar as datetime="10-17-2008"  'initialize however
dtVar.Month-1 will give you the right index for the dropdown for month

dtVar.Day-1 will do the same for day

for the year you will need to search the drop down for the matching year.

HTH,

Shane
0

Author Comment

ID: 22745986
silemone:

thanks for the code,  i have tried adding your script, it worked too...

i think i need to handle this situation in a different way, the new values are not been displayed as it is loading all the time from the Sub routines replacing what  has been captured ....

thanks ALL !!
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.