Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Date Conversion Problem

Date Conversion Problem

Scheduled Pinned Locked Moved ASP.NET
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    specialdreamsin
    wrote on last edited by
    #1

    Hi, I have a ASP.NET page, in which I have to compare two Dates. One comes from Database, and other one is selected from Calendar Control, assigned to textbox. When I code... If CDate(txtDespDate.Text) < CDate(MasterDispDt) Then LabelMsg.Text = "Invalid Date Selection" Exit Sub End If This is the Error I am getting, Error Message: Conversion from string "21-04-2009" to type 'Date' is not valid. Source : Microsoft.VisualBasic Stack Trace : at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value) at DocTransfers.BtnTransfer_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    B W H 3 Replies Last reply
    0
    • S specialdreamsin

      Hi, I have a ASP.NET page, in which I have to compare two Dates. One comes from Database, and other one is selected from Calendar Control, assigned to textbox. When I code... If CDate(txtDespDate.Text) < CDate(MasterDispDt) Then LabelMsg.Text = "Invalid Date Selection" Exit Sub End If This is the Error I am getting, Error Message: Conversion from string "21-04-2009" to type 'Date' is not valid. Source : Microsoft.VisualBasic Stack Trace : at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value) at DocTransfers.BtnTransfer_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Dim time1 As String = DateTime.Parse("2:15 PM").ToString("t") Dim time2 As String = DateTime.Now.ToString("t") If DateTime.Compare(DateTime.Parse(time1), DateTime.Parse(time2)) < 0 Then Response.Write("time1 is less than time2") Else Response.Write("time1 is greater than time2") End If


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      1 Reply Last reply
      0
      • S specialdreamsin

        Hi, I have a ASP.NET page, in which I have to compare two Dates. One comes from Database, and other one is selected from Calendar Control, assigned to textbox. When I code... If CDate(txtDespDate.Text) < CDate(MasterDispDt) Then LabelMsg.Text = "Invalid Date Selection" Exit Sub End If This is the Error I am getting, Error Message: Conversion from string "21-04-2009" to type 'Date' is not valid. Source : Microsoft.VisualBasic Stack Trace : at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value) at DocTransfers.BtnTransfer_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

        W Offline
        W Offline
        www Developerof NET
        wrote on last edited by
        #3

        use DateTime.ParseExact to convert your date in "dd/mm/yyyy" fromat to "mm/dd/yyyy" format, since sql only recognises the latter format. eg: Dim invoicedate As DateTime = DateTime.ParseExact(Inv_Date.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture)

        Hope it helps...

        When you fail to plan, you are planning to fail.

        1 Reply Last reply
        0
        • S specialdreamsin

          Hi, I have a ASP.NET page, in which I have to compare two Dates. One comes from Database, and other one is selected from Calendar Control, assigned to textbox. When I code... If CDate(txtDespDate.Text) < CDate(MasterDispDt) Then LabelMsg.Text = "Invalid Date Selection" Exit Sub End If This is the Error I am getting, Error Message: Conversion from string "21-04-2009" to type 'Date' is not valid. Source : Microsoft.VisualBasic Stack Trace : at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value) at DocTransfers.BtnTransfer_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

          H Offline
          H Offline
          Hajab
          wrote on last edited by
          #4

          To convert a string to date, the date format should be MM/DD/YYYY OR MM/DD/YY. The date "21-04-2009" is in the format dd-mm-yyyy. This is the reason for the error. Convert the string to correct format and your will get the result.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups