Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date function in vb.net

Status
Not open for further replies.

slowNsteady

Programmer
May 29, 2009
22
0
0
US
Hi,

Is there any date function available in vb.net to display the dates as today , yesterday, lastweek, last month like Microsoft outlook when we arrange it by date.

If there is no function available like that can anybody tell me how to do that.

i searched through the net but no luck.

Thanks

Sri

 

That should get you started.

Place a TextBox1, Label1 and Button1 on the Form, type a valid date in the text box and run this code:
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = SomeDate(CDate(TextBox1.Text))
    End Sub
[blue]
    Public Function SomeDate(ByRef datDate As Date) As String
        Dim strSomeDate As String = ""
        Dim intDiff As Integer = 0

        intDiff = DateDiff(DateInterval.Day, datDate, Date.Now)

        Select Case intDiff
            Case 0
                strSomeDate = "Today"
            Case 1
                strSomeDate = "Yesterday"
            Case Is > 7
                strSomeDate = "Last Week"
        End Select

        Return strSomeDate
    End Function[/blue]
End Class
I am sure other people will have better ideas, but that's a start.

Have fun.

---- Andy
 
If you look at DateTime, you will see a collection of Add functions that would probably be useful.

.AddDays
.AddMonths
.AddYears

are a few. You could then also extend DateTime to have a .Yesterday, .Tomorrow, .Whatever to fits your needs.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 

Thanks guys.your code helped me to find the solution

Here is the code.Sure this might help somebody in future

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = SomeDate(CDate(TextBox1.Text))
End Sub

Public Function SomeDate(ByRef datDate As Date) As String

Dim strSomeDate As String = ""
Dim intDiff As Integer = 0

intDiff = DateDiff(DateInterval.Day, datDate, Date.Now)

If intDiff = 0 Then
strSomeDate = "Today"
ElseIf intDiff = 1 Then
strSomeDate = "Yesterday"
ElseIf intDiff = 2 Then
strSomeDate = datDate.DayOfWeek.ToString
ElseIf intDiff = 3 Then
strSomeDate = datDate.DayOfWeek.ToString
ElseIf intDiff = 4 Then
strSomeDate = datDate.DayOfWeek.ToString
ElseIf intDiff = 5 Then
strSomeDate = datDate.DayOfWeek.ToString
ElseIf intDiff = 6 Then
strSomeDate = datDate.DayOfWeek.ToString
ElseIf intDiff >= 7 And intDiff < 14 Then
strSomeDate = "Last Week"
ElseIf intDiff >= 14 And intDiff < 21 Then
strSomeDate = "Two Weeks Ago"
ElseIf intDiff >= 21 And intDiff < 28 Then
strSomeDate = "Three Weeks Ago"
ElseIf intDiff > 28 And intDiff < 365 Then
strSomeDate = "One Month Ago"
ElseIf intDiff > 365 Then
strSomeDate = "One Year Ago"
End If

Return strSomeDate

End Function

cheers,
Sri



 
Better use Select Case for a more readable way (and easier to add new 'cases')
 
Here's my take on this (based somewhat on SlowNsteady's version):

Code:
Namespace CustomControls

    Public Class DateTimePicker
        Inherits System.Windows.Forms.DateTimePicker

        Public Function RelativeDate() As String

            Dim curDayOfWeek As Integer = DatePart(DateInterval.Weekday, Date.Now)
            Dim DaysDiff As Integer = curDayOfWeek - Math.Ceiling(DateDiff(DateInterval.Hour, Me.Value, Date.Now) / 24)

            Select Case DaysDiff
                Case curDayOfWeek
                    ' today
                    Return "Today"

                Case curDayOfWeek + 1 To 7
                    ' future, this week
                    Return (DaysDiff - curDayOfWeek).ToString & " Day(s) From Now"

                Case 8 To 28
                    ' future, less than a month
                    Return (DaysDiff \ 7).ToString & " Week(s) From Now"

                Case 29 To 365
                    ' future, less than a year; approximate
                    Return (DaysDiff \ 28).ToString & " Month(s) From Now"

                Case Is > 365
                    ' future, more than a year
                    Return (DaysDiff \ 365).ToString & " Year(s) From Now"

                Case 1 To curDayOfWeek - 1
                    ' past, this week
                    Return (curDayOfWeek - DaysDiff).ToString & " Day(s) Ago"

                Case -6 To 0
                    ' past, last week
                    Return "Last Week"

                Case -29 To -7
                    ' past, less than a month
                    Return System.Math.Abs((DaysDiff \ 7)).ToString & " Week(s) Ago"

                Case -366 To -30
                    ' past, less than a year
                    Return System.Math.Abs((DaysDiff \ 28)).ToString & " Month(s) Ago"

                Case Is < -366
                    ' past, more than a year
                    Return System.Math.Abs((DaysDiff \ 365)).ToString & " Year(s) Ago"

                Case Else
                    ' this shouldn't happen
                    Return "Error"
            End Select

        End Function

    End Class
End Namespace

Sub-class the system.windows.forms.datetime picker.
 
bah, I just realized I made a mistake.
Using that ceiling function when setting the value of DaysDiff works for any value except for the current day.
Something like this would fix it:
Code:
Dim DaysDiff As Integer

If DateDiff(DateInterval.Hour, Me.Value, Date.Now) / 24 >= 1 Then
        DaysDiff = curDayOfWeek - Math.Ceiling(DateDiff(DateInterval.Hour, Me.Value, Date.Now) / 24)
Else
        DaysDiff = 0
End If

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top