Solved

using a split function

Posted on 2008-10-17
16
243 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:learningnet
  • 6
  • 3
  • 2
  • +3
16 Comments
 
LVL 25

Accepted Solution

by:
SStory earned 100 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 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 100 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 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 100 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

by:silemone
silemone earned 100 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

by:silemone
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 59

Expert Comment

by:Kevin Cross
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

by:silemone
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

by:openshac
ID: 22740321
Cast the value as a DateTime and then use Day, Month and Year properties.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 21

Expert Comment

by:silemone
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

by:silemone
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 59

Expert Comment

by:Kevin Cross
ID: 22740399
You can also use the:
objDT.ToString().PadLeft(2, "0")
0
 

Author Comment

by:learningnet
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()
        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..

0
 
LVL 6

Assisted Solution

by:openshac
openshac earned 100 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

by:silemone
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

by:SStory
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

by:learningnet
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now