locked
Date comparison in vb.net RRS feed

  • Question

  • User-1578974752 posted

    Actually I want to show a gridview if Date.Now in between Datefrom.Text  and Dateto.Text .

    Date.Now= 25/9/2018

    Datefrom.Text= 20/9

    Dateto.Text = 30/9

    Vis.text = Date.Now

    Working fine as above .It is working fine for the same month

     ( But Dateto.Text  is Next 10 and Datefrom.Text =current month 20 then it is not working)
     If VIS.Text > Datefrom.Text And VIS.Text < Dateto.Text Then

    Tuesday, September 25, 2018 8:04 AM

Answers

  • User61956409 posted

    Hi shsu,

    If VIS.Text > Datefrom.Text And VIS.Text < Dateto.Text

    If you'd like to compare current_date, from_date and to_date, you can refer to the following code snippet, which works for me.

    Datefrom.Text = "20/9/2018"
    Dateto.Text = "10/10/2018"
    Vis.Text = DateTime.Now.ToString("dd/M/yyyy")
    
    Dim date_Vis As DateTime = DateTime.ParseExact(Vis.Text, "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    
    Dim date_from As DateTime = DateTime.ParseExact(Datefrom.Text, "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    
    Dim date_to As DateTime = DateTime.ParseExact(Dateto.Text, "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    
    Dim isture As Boolean = False
    
    
    If DateTime.Compare(date_Vis, date_from) > 0 And DateTime.Compare(date_to, date_Vis) > 0 Then
        isture = True
    
    End If

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2018 6:10 AM

All replies

  • User61956409 posted

    Hi shsu,

    If VIS.Text > Datefrom.Text And VIS.Text < Dateto.Text

    If you'd like to compare current_date, from_date and to_date, you can refer to the following code snippet, which works for me.

    Datefrom.Text = "20/9/2018"
    Dateto.Text = "10/10/2018"
    Vis.Text = DateTime.Now.ToString("dd/M/yyyy")
    
    Dim date_Vis As DateTime = DateTime.ParseExact(Vis.Text, "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    
    Dim date_from As DateTime = DateTime.ParseExact(Datefrom.Text, "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    
    Dim date_to As DateTime = DateTime.ParseExact(Dateto.Text, "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    
    Dim isture As Boolean = False
    
    
    If DateTime.Compare(date_Vis, date_from) > 0 And DateTime.Compare(date_to, date_Vis) > 0 Then
        isture = True
    
    End If

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2018 6:10 AM
  • User-1578974752 posted

    Thanks for the help

    Friday, October 5, 2018 11:23 AM