Simplified way to assign date values

Hi all,

i have a Form that allows the user to enter an Hour and Minute value, which I then need to convert into an actual date value, which I am doing as follows...

objInspectionHeader.m_dtInspectionDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, intHour, intMinute, DateTime.Now.Second);

But to me, this seems very long winded. Is there a more simplified way that I can just assign the entered Hour and Minute value to a date current date

Thanks



Answer this question

Simplified way to assign date values

  • Etowah_man

    You can do this

    DateTime.Now.Date.AddHours(intHour).AddMinutes(intMinute)

     



  • TheAgent

    Thanks for the reply. But doesn't that add the hours and minutes onto the current datetime

    For example:

    Now = 19-01-2007 13:00:00
    DateTime.Now.Date.AddHours(3).AddMinutes(30) = 19-01-2007 16:30:00

    I need the hour and minute value replacing their respective values returned from Now().

    Thanks


  • kart

    Then do this:

    DateTime.Today.Data.AddHours(int).AddMinutes(int);


  • Mikayla

    When using any .NET language, you are expected to write long expressions. I solved your problem like following code snippet

    // DateTime midnightDT = new DateTime( 2006, 9, 24);

    DateTime midnightDT = DateTime.Today;

    Console.WriteLine( "Midnight {0}", midnightDT.ToString("r") );

    // You need new DateTime  laterDT to here, because AddXxxx don't alter original midnightDT object

    DateTime laterDT = midnightDT.AddHours(3).AddMinutes(30);

    Console.WriteLine( "Three and half hours later {0}", laterDT.ToString("r") );

    Maybe there is too much to write, but we are humans and need do this way.



  • Simplified way to assign date values