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. Convert DateTime to Date

Convert DateTime to Date

Scheduled Pinned Locked Moved C#
help
16 Posts 6 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.
  • V Verghese

    In my DataSet, I have a field called "ADMISSION_DATE" in the format of DateTime, which upon displaying in my Report showz up as, lets say, "08-29-08 12:00:00". 3rd Line displays the Date (08-30-08) as required but 4th line displays it as DateTime (08-29-08 12:00:00). But I want only the date thing (i.e. 08-29-08). And in dataset I don't see any direct DATE conversion methods. Please help.

    row["ADMISSION_DATE"] = DateTime.Parse(row["MM"].ToString() + "/" + row["DD"].ToString() + "/" + row["CC"].ToString() + row["YY"].ToString().ToString()) ;

    DateTime newDT = new DateTime(); //Line-1
    newDT = Convert.ToDateTime(row["ADMISSION_DATE"]).Date; //Line-2
    MessageBox.Show(newDT.ToShortDateString()); //Line-3

    row["ADM_DATE"] = newDT.ToShortDateString(); //Line-4

    Thanking you in anticipation.

    G Offline
    G Offline
    Guffa
    wrote on last edited by
    #3

    A DateTime value always has a time component, even if it's zero. The Date property returns a DateTime value where the time component is set to zero, just as you do in line 2.

    Verghese wrote:

    row["ADMISSION_DATE"] = DateTime.Parse(row["MM"].ToString() + "/" + row["DD"].ToString() + "/" + row["CC"].ToString() + row["YY"].ToString().ToString()) ;

    If you already have the components of the date as numbers, don't format them into a string just to parse into a date. There is a perfectly good constructor for the DateTime structure that takes numbers: row["ADMISSION_DATE"] = new DateTime((int)row["CC"] * 100 + (int)row["YY], (int)row["MM"], (int)row["DD"]);

    Verghese wrote:

    3rd Line displays the Date (08-30-08) as required but 4th line displays it as DateTime (08-29-08 12:00:00).

    You are storing a string in the field, so if you display it as it is, it can't display in any other way than the way that you formatted it. The only chance that it would display with a time component, is if it's parsed into a DateTime value and the formatted into a string again using a different format.

    Despite everything, the person most likely to be fooling you next is yourself.

    V 1 Reply Last reply
    0
    • V Verghese

      In my DataSet, I have a field called "ADMISSION_DATE" in the format of DateTime, which upon displaying in my Report showz up as, lets say, "08-29-08 12:00:00". 3rd Line displays the Date (08-30-08) as required but 4th line displays it as DateTime (08-29-08 12:00:00). But I want only the date thing (i.e. 08-29-08). And in dataset I don't see any direct DATE conversion methods. Please help.

      row["ADMISSION_DATE"] = DateTime.Parse(row["MM"].ToString() + "/" + row["DD"].ToString() + "/" + row["CC"].ToString() + row["YY"].ToString().ToString()) ;

      DateTime newDT = new DateTime(); //Line-1
      newDT = Convert.ToDateTime(row["ADMISSION_DATE"]).Date; //Line-2
      MessageBox.Show(newDT.ToShortDateString()); //Line-3

      row["ADM_DATE"] = newDT.ToShortDateString(); //Line-4

      Thanking you in anticipation.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #4

      Dates and times should always be stored as DateTime (when available). If they aren't, then you run into these kinds of problems. Any formatting should be performed when the value is displayed (or written), not in the database. And please use an ISO 8601-compliant format, e.g. YYYY-MM-DD

      1 Reply Last reply
      0
      • G Guffa

        A DateTime value always has a time component, even if it's zero. The Date property returns a DateTime value where the time component is set to zero, just as you do in line 2.

        Verghese wrote:

        row["ADMISSION_DATE"] = DateTime.Parse(row["MM"].ToString() + "/" + row["DD"].ToString() + "/" + row["CC"].ToString() + row["YY"].ToString().ToString()) ;

        If you already have the components of the date as numbers, don't format them into a string just to parse into a date. There is a perfectly good constructor for the DateTime structure that takes numbers: row["ADMISSION_DATE"] = new DateTime((int)row["CC"] * 100 + (int)row["YY], (int)row["MM"], (int)row["DD"]);

        Verghese wrote:

        3rd Line displays the Date (08-30-08) as required but 4th line displays it as DateTime (08-29-08 12:00:00).

        You are storing a string in the field, so if you display it as it is, it can't display in any other way than the way that you formatted it. The only chance that it would display with a time component, is if it's parsed into a DateTime value and the formatted into a string again using a different format.

        Despite everything, the person most likely to be fooling you next is yourself.

        V Offline
        V Offline
        Verghese
        wrote on last edited by
        #5

        Guffa, I understand that, storing a value as string cannot be displayed in any other formats unless until we parse it into some other formats. But that's the reason why I'm parsing it into DateTime format. see the code one again......

        row["ADMISSION_DATE"] = DateTime.Parse(row["MM"].ToString() + "/" + row["DD"].ToString() + "/" + row["CC"].ToString() + row["YY"].ToString().ToString()) ;

        The row["MM"].ToString is a String, but I'm finally parsing it into DateTime. And after execution I could see that the "ADMISSION_DATE" field type in DataSet changes to DateTime. So from this its clear that it's getting stored as DateTime and the field type is also DateTime. But the problem is to extract only the date thing, when I call the DataSet rows. Hope I dindn't confused you.

        G 1 Reply Last reply
        0
        • V Verghese

          In my DataSet, I have a field called "ADMISSION_DATE" in the format of DateTime, which upon displaying in my Report showz up as, lets say, "08-29-08 12:00:00". 3rd Line displays the Date (08-30-08) as required but 4th line displays it as DateTime (08-29-08 12:00:00). But I want only the date thing (i.e. 08-29-08). And in dataset I don't see any direct DATE conversion methods. Please help.

          row["ADMISSION_DATE"] = DateTime.Parse(row["MM"].ToString() + "/" + row["DD"].ToString() + "/" + row["CC"].ToString() + row["YY"].ToString().ToString()) ;

          DateTime newDT = new DateTime(); //Line-1
          newDT = Convert.ToDateTime(row["ADMISSION_DATE"]).Date; //Line-2
          MessageBox.Show(newDT.ToShortDateString()); //Line-3

          row["ADM_DATE"] = newDT.ToShortDateString(); //Line-4

          Thanking you in anticipation.

          V Offline
          V Offline
          vikas amin
          wrote on last edited by
          #6

          you said Reports So if its just a question about displaying the data on the report , use string functions to eliminate the extra time .

          Vikas Amin

          My First Article on CP" Virtual Serail Port "[^]

          V 1 Reply Last reply
          0
          • V Verghese

            Guffa, I understand that, storing a value as string cannot be displayed in any other formats unless until we parse it into some other formats. But that's the reason why I'm parsing it into DateTime format. see the code one again......

            row["ADMISSION_DATE"] = DateTime.Parse(row["MM"].ToString() + "/" + row["DD"].ToString() + "/" + row["CC"].ToString() + row["YY"].ToString().ToString()) ;

            The row["MM"].ToString is a String, but I'm finally parsing it into DateTime. And after execution I could see that the "ADMISSION_DATE" field type in DataSet changes to DateTime. So from this its clear that it's getting stored as DateTime and the field type is also DateTime. But the problem is to extract only the date thing, when I call the DataSet rows. Hope I dindn't confused you.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #7

            Yes, you are storing a DateTime value in the field, and the value has a time component that is zero. Then you read the field, convert the DateTime value to a DateTime value, and use the Date property to get a DateTime value where the time component is zero from the DateTime value where the time component already is zero. Then you convert the value into a string using a format that does not even include the time component that is zero. As the value that you finally store as a string in a field doesn't have any time information at all, the problem is not in the code that you have shown. As I said before, the only chance that this value is displayed with a time, is if it's once again parsed into a DateTime value and then formatted into a string with a different format.

            Despite everything, the person most likely to be fooling you next is yourself.

            1 Reply Last reply
            0
            • V Verghese

              In my DataSet, I have a field called "ADMISSION_DATE" in the format of DateTime, which upon displaying in my Report showz up as, lets say, "08-29-08 12:00:00". 3rd Line displays the Date (08-30-08) as required but 4th line displays it as DateTime (08-29-08 12:00:00). But I want only the date thing (i.e. 08-29-08). And in dataset I don't see any direct DATE conversion methods. Please help.

              row["ADMISSION_DATE"] = DateTime.Parse(row["MM"].ToString() + "/" + row["DD"].ToString() + "/" + row["CC"].ToString() + row["YY"].ToString().ToString()) ;

              DateTime newDT = new DateTime(); //Line-1
              newDT = Convert.ToDateTime(row["ADMISSION_DATE"]).Date; //Line-2
              MessageBox.Show(newDT.ToShortDateString()); //Line-3

              row["ADM_DATE"] = newDT.ToShortDateString(); //Line-4

              Thanking you in anticipation.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              See example : DateTime d = new DateTime(2008, 8, 29, 12, 0, 0); Console.WriteLine(d.ToString("dd-MM-yy"));

              1 Reply Last reply
              0
              • V vikas amin

                you said Reports So if its just a question about displaying the data on the report , use string functions to eliminate the extra time .

                Vikas Amin

                My First Article on CP" Virtual Serail Port "[^]

                V Offline
                V Offline
                Verghese
                wrote on last edited by
                #9

                Hi Vikas, if the task wud have been just to display the data on the report, thn it was easy. See here's just the gist, my database table has dates splitted in 4 columns like DD, MM, CC, YY. Then using DataSet I'm concatenating these fields. And I'm concatenating it, bcoz I want to display the records within a particular date range. And for tht date comparison thing, it has to be in the date format and not string format. And this comparison has to be done at the code level and not at the database level, since database doesnt have anything like a date field.

                V P 2 Replies Last reply
                0
                • V Verghese

                  Hi Vikas, if the task wud have been just to display the data on the report, thn it was easy. See here's just the gist, my database table has dates splitted in 4 columns like DD, MM, CC, YY. Then using DataSet I'm concatenating these fields. And I'm concatenating it, bcoz I want to display the records within a particular date range. And for tht date comparison thing, it has to be in the date format and not string format. And this comparison has to be done at the code level and not at the database level, since database doesnt have anything like a date field.

                  V Offline
                  V Offline
                  vikas amin
                  wrote on last edited by
                  #10

                  can you try row["ADM_DATE"] = newDT.ToShortDateString().Remove(9)

                  Vikas Amin

                  My First Article on CP" Virtual Serial Port "[^]

                  modified on Thursday, July 24, 2008 5:33 PM

                  V 1 Reply Last reply
                  0
                  • V Verghese

                    Hi Vikas, if the task wud have been just to display the data on the report, thn it was easy. See here's just the gist, my database table has dates splitted in 4 columns like DD, MM, CC, YY. Then using DataSet I'm concatenating these fields. And I'm concatenating it, bcoz I want to display the records within a particular date range. And for tht date comparison thing, it has to be in the date format and not string format. And this comparison has to be done at the code level and not at the database level, since database doesnt have anything like a date field.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #11

                    Verghese wrote:

                    database table has dates splitted in 4 columns like DD, MM, CC, YY

                    Which is a very poor design.

                    Verghese wrote:

                    database doesnt have anything like a date field

                    What database doesn't have a DateTime type?

                    V 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Verghese wrote:

                      database table has dates splitted in 4 columns like DD, MM, CC, YY

                      Which is a very poor design.

                      Verghese wrote:

                      database doesnt have anything like a date field

                      What database doesn't have a DateTime type?

                      V Offline
                      V Offline
                      Verghese
                      wrote on last edited by
                      #12

                      Dear........this is not my design, its the way how dates are always stored in AS400 Mainframe systems. I'm working on IBM iSeries Mainframe server. I have not much options on choosing the database. Did I cleared your doubt???

                      P 1 Reply Last reply
                      0
                      • V vikas amin

                        can you try row["ADM_DATE"] = newDT.ToShortDateString().Remove(9)

                        Vikas Amin

                        My First Article on CP" Virtual Serial Port "[^]

                        modified on Thursday, July 24, 2008 5:33 PM

                        V Offline
                        V Offline
                        Verghese
                        wrote on last edited by
                        #13

                        Vikas, whn i try this line

                        row["ADM_DATE"] = newDT.ToShortDateString().Remove(9)

                        it's throwing up this error:

                        "No overload for method 'Remove' takes '1' arguments"

                        V 1 Reply Last reply
                        0
                        • V Verghese

                          Dear........this is not my design, its the way how dates are always stored in AS400 Mainframe systems. I'm working on IBM iSeries Mainframe server. I have not much options on choosing the database. Did I cleared your doubt???

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #14

                          Had you said that first, you would have gotten more sympathy. :-D

                          V 1 Reply Last reply
                          0
                          • V Verghese

                            Vikas, whn i try this line

                            row["ADM_DATE"] = newDT.ToShortDateString().Remove(9)

                            it's throwing up this error:

                            "No overload for method 'Remove' takes '1' arguments"

                            V Offline
                            V Offline
                            vikas amin
                            wrote on last edited by
                            #15

                            not sure why but you should use the String::Remove Method (Int32, Int32) http://msdn.microsoft.com/en-us/library/d8d7z2kk.aspx[^]

                            Vikas Amin

                            My First Article on CP" Virtual Serial Port "[^]

                            modified on Thursday, July 24, 2008 5:33 PM

                            1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              Had you said that first, you would have gotten more sympathy. :-D

                              V Offline
                              V Offline
                              Verghese
                              wrote on last edited by
                              #16

                              dear friend, i didnt knew u were this caring...........but dont be in the assumption that it was way too funny...........

                              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