Link to home
Start Free TrialLog in
Avatar of learningnet
learningnet

asked on

using a split function

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..

please advise

thanks in advance
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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
just to make clear;  all objDT.Day, objDT.Month, and objDT.Year returns ints so if you're using string just at +""
or toString()
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
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
Avatar of openshac
openshac

Cast the value as a DateTime and then use Day, Month and Year properties.
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
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


You can also use the:
objDT.ToString().PadLeft(2, "0")
Avatar of learningnet

ASKER

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()
        lst.Items.Add(New ListItem("-", "0"))
        For i = first To last
            lst.Items.Add(New ListItem(i, i))
        Next
    End Sub


Public Shared Sub FillDropDownListWithMonth(ByVal lst As DropDownList)
        lst.Items.Add(New ListItem("-", "0"))
        For i As Integer = 1 To 12
            lst.Items.Add(New ListItem(MonthName(i), i))
        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..

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
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
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 !!