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. General Programming
  3. C#
  4. C#.net date Conversion Problem

C#.net date Conversion Problem

Scheduled Pinned Locked Moved C#
csharphelp
5 Posts 5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hello All , Is There any way to Convert the Date in one Format to Another Format I am taking Date in dd/MM/yyyy Format from my Calender to TextBox :confused::confused:Now I need to take this date from TextBox as a string and Convert to dateTime (i.e Convert.TodateTime(TextBox1.Text)) Having MM/dd/yyyy(i.e.System Specified ) Format Thanks for any help anyone is able to give me.

    H V I R 4 Replies Last reply
    0
    • L Lost User

      Hello All , Is There any way to Convert the Date in one Format to Another Format I am taking Date in dd/MM/yyyy Format from my Calender to TextBox :confused::confused:Now I need to take this date from TextBox as a string and Convert to dateTime (i.e Convert.TodateTime(TextBox1.Text)) Having MM/dd/yyyy(i.e.System Specified ) Format Thanks for any help anyone is able to give me.

      H Offline
      H Offline
      Harini N K
      wrote on last edited by
      #2

      Hi For datetime function, it always take this format MM/dd/yyyy. Call this function (added below) if your date format is dd/MM/yyyy:

      public string ParseDateTime(string StrDate)
      {
      String StrReturnDate = "";
      String StrDay = "";
      String StrMonth = "";
      String StrYear = "";
      StrDay = StrDate.Substring(0, 2);
      StrMonth = StrDate.Substring((StrDate.IndexOf("/") + 1), 2);
      StrYear = StrDate.Substring((StrDate.LastIndexOf("/") + 1), 4);
      StrReturnDate = StrMonth + "/" + StrDay + "/" + StrYear;
      return StrReturnDate;
      }

      Harini

      1 Reply Last reply
      0
      • L Lost User

        Hello All , Is There any way to Convert the Date in one Format to Another Format I am taking Date in dd/MM/yyyy Format from my Calender to TextBox :confused::confused:Now I need to take this date from TextBox as a string and Convert to dateTime (i.e Convert.TodateTime(TextBox1.Text)) Having MM/dd/yyyy(i.e.System Specified ) Format Thanks for any help anyone is able to give me.

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        Try this: string DATETIMEPATTERN = "dd/MM/yyyy"; string strdate = "28/02/2007"; DateTime mydatetime; DateTime dt; DateTime.TryParseExact(strdate, DATETIMEPATTERN, null, DateTimeStyles.None, out dt); mydatetime = dt;

        V. I found a living worth working for, but haven't found work worth living for.

        1 Reply Last reply
        0
        • L Lost User

          Hello All , Is There any way to Convert the Date in one Format to Another Format I am taking Date in dd/MM/yyyy Format from my Calender to TextBox :confused::confused:Now I need to take this date from TextBox as a string and Convert to dateTime (i.e Convert.TodateTime(TextBox1.Text)) Having MM/dd/yyyy(i.e.System Specified ) Format Thanks for any help anyone is able to give me.

          I Offline
          I Offline
          IamMohan
          wrote on last edited by
          #4

          Hi, if you getting the date as dd/MM/YYYY format(e.g, 31/01/2007) convert it to the .NET compliant date format like, 31/JAN/2007. You can simply do so by calling the function GetMonth. string dateValue = "31/01/2007"; string month = GetMonth(dateValue.Split('/')[1]); Then perform the following DateTime.Parse(stringDateTime); You can easily set the datetime for the corresponding string public string GetMonth(string monthNumber) { string [] monthName = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Nov","Dec"}; return monthName[Int32.Parse(monthNumber)- 1]; } -- modified at 4:58 Wednesday 28th February, 2007

          Mohan

          1 Reply Last reply
          0
          • L Lost User

            Hello All , Is There any way to Convert the Date in one Format to Another Format I am taking Date in dd/MM/yyyy Format from my Calender to TextBox :confused::confused:Now I need to take this date from TextBox as a string and Convert to dateTime (i.e Convert.TodateTime(TextBox1.Text)) Having MM/dd/yyyy(i.e.System Specified ) Format Thanks for any help anyone is able to give me.

            R Offline
            R Offline
            Rhys Gravell
            wrote on last edited by
            #5

            What you're saying doesn't make a great deal of sense as a DateTime is a DateTime is a DateTime, formatting is irrelevant. Do you actually mean that you want the string equivalent of a DateTime string value in an alternate string format? EG; private string FormatDateTimeString( string dateTimeString ) { DateTime dateTime = DateTime.Now; if( DateTime.TryParse( dateTimeString, out dateTime ) ) { return dateTime.ToString( "MM/dd/yyyy" ); } else { throw new FormatException( "The string value, (" + dateTimeString + "), passed to the 'FormatDateTimeString( string dateTimeString )' method is not a valid DateTime" ); } }

            Rhys

            ELYSIUM, n. An imaginary delightful country which the ancients foolishly believed to be inhabited by the spirits of the good. This ridiculous and mischievous fable was swept off the face of the earth by the early Christians -- may their souls be happy in Heaven!

            Ambrose Bierce (1842 - 1914)

            Behind every argument is someone's ignorance.

            Louis D. Brandeis (1856 - 1941)

            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