10 Replies - 1526 Views - Last Post: 10 December 2012 - 01:13 PM Rate Topic: -----

#1 uttar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-October 12

if else in datetime in vb.net

Posted 26 November 2012 - 08:48 PM

hi

i want to output 2years.or 2years 3months 6days.rather than 2years 0months and 0days if One of them a zeros 0(s) i dont want to display. ..i have my code below..please can you help me on this..im new to vb..
Public Function GetCustomDate(ByVal days As Int32) As String
        
        Dim timeSpan as System.TimeSpan
        'timeSpan = objDate2 - objDate1
        Dim objDate1 As DateTime = DateTime.Now()
        Dim objDate2 As DateTime = objDate1.AddDays(-1 * days)        
       
Return (objDate1.Year - objDate2.Year)& "Years" &(objDate1.Month - objDate2.Month) &"Months"&(objDate1.Day - objDate2.Day)& " Days" 
  

End Function





thanks

Is This A Good Question/Topic? 0
  • +

Replies To: if else in datetime in vb.net

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: if else in datetime in vb.net

Posted 26 November 2012 - 09:14 PM

Personally, I'd use a Select Case. It may sound a little odd, but consider that there are 3 cases, not just two, for consideration when building the string.

Case 0: output nothing
Case 1: output "1 Month" (or year, or day)
Case Else: output "n Months" (or "Years" or "Days"

This will help make it sound right when reading it. Additionally, you probably don't want to jam the number together with the text, so it would be nice to put in spaces and commas.

Here's a partial sample:
        Dim years, months, days As Integer
        Dim Y, M, D As String

        Select Case years
            Case 0
                Y = ""
            Case 1
                Y = "1 Year"
            Case Else
                Y = Y.ToString & " Years"
        End Select


Do the same for Months and Days, but when you do months, check to see if Years has anything to output. If so, stick a comma in the output generated by Months (in the appropriate spot, of course). Same thing for Days, but you need to check both years and months to see if you need a comma.

After the three Sekect Case statements, all you need to do is to Return(Y & M & D)
Was This Post Helpful? 0
  • +
  • -

#3 uttar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-October 12

Re: if else in datetime in vb.net

Posted 28 November 2012 - 03:02 PM

hi

if i got 230days it should be 7months 20days but i got 7 months 16days its not give me d the right result in days.but this problem always happens to my days only and not years and months,secondly my days too got negative for some like



Public Function XX(ByVal days As integer) As String
        
     Dim objDate2 As DateTime = objDate1.AddDays(-1* days)    
     dim result as String=0

        If Not (objDate1.Day - objDate2.Day)=0  Then
            result += (objDate1.Day - objDate2.Day) & " Days "
             'result+
        End If

        Return result.Trim()




thanks in advance
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: if else in datetime in vb.net

Posted 28 November 2012 - 04:02 PM

First, this is a separate question. You have asked one question, and received an answer. Did this answer help you? Did you solve the problem. We'd really like to know. If you have another, unrelated question, you should be starting a new topic.

Now i'll ask you a question... What is in objDate1? Place a breakpoint on the first line of the XX() Function and single-step through it. Your answer will be the Day value in objDate1 - the Day value in objDate2. So if objDate1 has a date of #12/27/2012#, objDate2 will be assigned a value of #5/11/2012#, and your function WILL return 16, as it should (27-11).

Now suppose objDate1, on entry to XX() contains #9/27/2012#. Add - 230 days to that, and assign it to objDate2, then objDate2 will contain #2/10/2012#, and your function will return 17, as it should.

These dates are calculated as actual dates and days, not as some idealized 30-day month or 30.xxx-day month. If you are working with dates and times, use the Date and Time facilities of VB.Net. Don't try to cut a board with a screwdriver.
Was This Post Helpful? 0
  • +
  • -

#5 nilkanthnaik5  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 16
  • Joined: 22-October 12

Re: if else in datetime in vb.net

Posted 29 November 2012 - 11:01 PM

Hi uttar.
I am answering your first question.
I am giving you 2 solutions.
I think 1st is better.
Try first solution:-


Public Function GetCustomDate(ByVal days As Int32) As String
        Dim objDate1 As DateTime = DateTime.Now()
        Dim objDate2 As DateTime = objDate1.AddDays(-1 * days)
        Dim str1, str2, str3, strfinal As String
        str1 = (objDate1.Year - objDate2.Year) & " Years"
        str2 = (objDate1.Month - objDate2.Month) & " Months"
        str3 = (objDate1.Day - objDate2.Day) & " Days"
        If str1 <> "0 Years" Then
            strfinal = strfinal + str1
        End If

        If str2 <> "0 Months" Then
            strfinal = strfinal + str2
        End If
        If str3 <> "0 Days" Then
            strfinal = strfinal + str3
        End If

        Return strfinal
End Function




Try Second solution:-

Public Function GetCustomDate(ByVal days As Int32) As String

Dim objDate1 As DateTime = DateTime.Now()
Dim objDate2 As DateTime = objDate1.AddDays(-1 * days)

If (objDate1.Year - objDate2.Year) <> 0 And (objDate1.Month - objDate2.Month) <> 0 And (objDate1.Day - objDate2.Day) <> 0 Then
            Return (objDate1.Year - objDate2.Year) & "Years" & (objDate1.Month - objDate2.Month) & "Months" & (objDate1.Day - objDate2.Day) & " Days"
 
ElseIf (objDate1.Year - objDate2.Year) = 0 And (objDate1.Month - objDate2.Month) <> 0 And (objDate1.Day - objDate2.Day) <> 0 Then
            Return (objDate1.Month - objDate2.Month) & "Months" & (objDate1.Day - objDate2.Day) & " Days"

ElseIf (objDate1.Year - objDate2.Year) <> 0 And (objDate1.Month - objDate2.Month) = 0 And (objDate1.Day - objDate2.Day) <> 0 Then
            Return (objDate1.Year - objDate2.Year) & "Years" & (objDate1.Day - objDate2.Day) & " Days"

ElseIf (objDate1.Year - objDate2.Year) <> 0 And (objDate1.Month - objDate2.Month) <> 0 And (objDate1.Day - objDate2.Day) = 0 Then
            Return (objDate1.Year - objDate2.Year) & "Years" & (objDate1.Month - objDate2.Month) & "Months"

ElseIf (objDate1.Year - objDate2.Year) <> 0 And (objDate1.Month - objDate2.Month) = 0 And (objDate1.Day - objDate2.Day) = 0 Then
            Return (objDate1.Year - objDate2.Year) & "Years"

ElseIf (objDate1.Year - objDate2.Year) = 0 And (objDate1.Month - objDate2.Month) <> 0 And (objDate1.Day - objDate2.Day) = 0 Then
            Return (objDate1.Month - objDate2.Month) & "Months"

ElseIf (objDate1.Year - objDate2.Year) = 0 And (objDate1.Month - objDate2.Month) = 0 And (objDate1.Day - objDate2.Day) <> 0 Then
            Return (objDate1.Day - objDate2.Day) & " Days"

ElseIf (objDate1.Year - objDate2.Year) = 0 And (objDate1.Month - objDate2.Month) = 0 And (objDate1.Day - objDate2.Day) = 0 Then
            Return (objDate1.Day - objDate2.Day) & " Days"
End If

End Function


Was This Post Helpful? 1
  • +
  • -

#6 uttar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-October 12

Re: if else in datetime in vb.net

Posted 30 November 2012 - 02:52 PM

hi nilkanthnaik5,

thanks for your reply..i was being able to solve it as dong some changes.i go for the 1st solution above as my reference..

Many thanks
Was This Post Helpful? 0
  • +
  • -

#7 uttar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-October 12

Re: if else in datetime in vb.net

Posted 09 December 2012 - 07:24 PM

View Postnilkanthnaik5, on 29 November 2012 - 11:01 PM, said:

Hi uttar.
I am answering your first question.
I am giving you 2 solutions.
I think 1st is better.
Try first solution:-


Public Function GetCustomDate(ByVal days As Int32) As String
        Dim objDate1 As DateTime = DateTime.Now()
        Dim objDate2 As DateTime = objDate1.AddDays(-1 * days)
        Dim str1, str2, str3, strfinal As String
        str1 = (objDate1.Year - objDate2.Year) & " Years"
        str2 = (objDate1.Month - objDate2.Month) & " Months"
        str3 = (objDate1.Day - objDate2.Day) & " Days"
        If str1 <> "0 Years" Then
            strfinal = strfinal + str1
        End If

        If str2 <> "0 Months" Then
            strfinal = strfinal + str2
        End If
        If str3 <> "0 Days" Then
            strfinal = strfinal + str3
        End If

        Return strfinal
End Function




Try Second solution:-

Public Function GetCustomDate(ByVal days As Int32) As String

Dim objDate1 As DateTime = DateTime.Now()
Dim objDate2 As DateTime = objDate1.AddDays(-1 * days)

If (objDate1.Year - objDate2.Year) <> 0 And (objDate1.Month - objDate2.Month) <> 0 And (objDate1.Day - objDate2.Day) <> 0 Then
            Return (objDate1.Year - objDate2.Year) & "Years" & (objDate1.Month - objDate2.Month) & "Months" & (objDate1.Day - objDate2.Day) & " Days"
 
ElseIf (objDate1.Year - objDate2.Year) = 0 And (objDate1.Month - objDate2.Month) <> 0 And (objDate1.Day - objDate2.Day) <> 0 Then
            Return (objDate1.Month - objDate2.Month) & "Months" & (objDate1.Day - objDate2.Day) & " Days"

ElseIf (objDate1.Year - objDate2.Year) <> 0 And (objDate1.Month - objDate2.Month) = 0 And (objDate1.Day - objDate2.Day) <> 0 Then
            Return (objDate1.Year - objDate2.Year) & "Years" & (objDate1.Day - objDate2.Day) & " Days"

ElseIf (objDate1.Year - objDate2.Year) <> 0 And (objDate1.Month - objDate2.Month) <> 0 And (objDate1.Day - objDate2.Day) = 0 Then
            Return (objDate1.Year - objDate2.Year) & "Years" & (objDate1.Month - objDate2.Month) & "Months"

ElseIf (objDate1.Year - objDate2.Year) <> 0 And (objDate1.Month - objDate2.Month) = 0 And (objDate1.Day - objDate2.Day) = 0 Then
            Return (objDate1.Year - objDate2.Year) & "Years"

ElseIf (objDate1.Year - objDate2.Year) = 0 And (objDate1.Month - objDate2.Month) <> 0 And (objDate1.Day - objDate2.Day) = 0 Then
            Return (objDate1.Month - objDate2.Month) & "Months"

ElseIf (objDate1.Year - objDate2.Year) = 0 And (objDate1.Month - objDate2.Month) = 0 And (objDate1.Day - objDate2.Day) <> 0 Then
            Return (objDate1.Day - objDate2.Day) & " Days"

ElseIf (objDate1.Year - objDate2.Year) = 0 And (objDate1.Month - objDate2.Month) = 0 And (objDate1.Day - objDate2.Day) = 0 Then
            Return (objDate1.Day - objDate2.Day) & " Days"
End If

End Function




if i have 1 months 1 days..my output is 1 months 2 days..but if i have 1 year 1 day, i display the right result which is 1 year 1 days..or 1 year 3months 1 day output is 1 year 3months 2 days..

how come i get 2 days..really need your help.

Public Function RaceDate(length  As Integer ) As String
        
             dim year As string=0
             dim month As string=0
            dim day As string=0
             dim ReturnString As string

ReturnString=""

If length = 0 then
Return String.Empty
end if



if length >=365
year=cstr((Math.Floor (length / 365)))
length  =(length  Mod 365)

end if

If length >=30 AndAlso length<365 Then
month =(cstr((Math.Floor(length/30))))
length  =(length  Mod 30)

end if

if length<=30 Then
day =cstr((length))

end if

if cint(year)>0

ReturnString= year+"  Years  "

end if

if cint(month)>0

ReturnString= ReturnString +month+" Months "

end if

if cint(day)>0

ReturnString=ReturnString+day+"  Days "
end if

return(ReturnString)

end Function

Was This Post Helpful? 0
  • +
  • -

#8 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: if else in datetime in vb.net

Posted 09 December 2012 - 10:05 PM

Place a break point. Do you know how to do that?
Was This Post Helpful? 0
  • +
  • -

#9 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: if else in datetime in vb.net

Posted 09 December 2012 - 10:54 PM

Quote

if i have 1 months 1 days..my output is 1 months 2 days..but if i have 1 year 1 day, i display the right result which is 1 year 1 days..or 1 year 3months 1 day output is 1 year 3months 2 days..

how come i get 2 days..really need your help.

I tested your code (Function RaceDate), and if I pass it 31, it returns "1 Months 1 Days", and if I pass it 456, it returns "1 Years 3 Months 1 Days".

So, where's the problem?
Was This Post Helpful? 0
  • +
  • -

#10 uttar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-October 12

Re: if else in datetime in vb.net

Posted 10 December 2012 - 12:17 PM

View Postlar3ry, on 09 December 2012 - 10:54 PM, said:

Quote

if i have 1 months 1 days..my output is 1 months 2 days..but if i have 1 year 1 day, i display the right result which is 1 year 1 days..or 1 year 3months 1 day output is 1 year 3months 2 days..

how come i get 2 days..really need your help.

I tested your code (Function RaceDate), and if I pass it 31, it returns "1 Months 1 Days", and if I pass it 456, it returns "1 Years 3 Months 1 Days".

So, where's the problem?



hi

thanks for your reply...


i enter 1 year 3months 1 day..and it return 1 year 3months 2 days...if i enter 2 days it returns 2 days..1 year 2days it return 1 year 2days but if i enter 3 months 2 days it return 3months 3 days...that's my problem now

thanks

View Posttrevster344, on 09 December 2012 - 10:05 PM, said:

Place a break point. Do you know how to do that?


hi trevster344,

dont know how to do that..is it same as to comments?

thanks
Was This Post Helpful? 0
  • +
  • -

#11 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: if else in datetime in vb.net

Posted 10 December 2012 - 01:13 PM

Quote

i enter 1 year 3months 1 day..and it return 1 year 3months 2 days...if i enter 2 days it returns 2 days..1 year 2days it return 1 year 2days but if i enter 3 months 2 days it return 3months 3 days...that's my problem now

You say you entered 1 year 3 months 1 day, but the function you supplied in your most recent code posting was Public Function RaceDate(length As Integer ) As String, and it doesn't take three values. It takes 1 value, and from your code, it takes a number of days. According to your code, again, 1 year is 365 days, 1 month is 30 days, and the only way you can put 1 year 3 months 1 day into that function is to call it with a value of 456 (365 + 90 + 1), and that, my friend, returns 1 year 3 months 1 day. So would you care to clarify how you are calling that function, and with what particular value, such that you get 1 year 3 months 1 day back from it?

View Postuttar, on 10 December 2012 - 01:17 PM, said:

View Posttrevster344, on 09 December 2012 - 10:05 PM, said:

Place a break point. Do you know how to do that?

dont know how to do that..is it same as to comments?

You should read the Debugging Skills article.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1