Set hour, minute, second to Date object

Hi all,

This is my first post here. I’m learning now about C#.
I need to kown how to Set hour, minute, second to Date object

If I try to use the follwoing, occurs an error:

[code]
DateTime dt = DateTime.Now;

dt.Hour = 0;
dt.Minute = 0;
dt.Second = 0;
dt.Millisecond = 0;
[/code]

when the Hour it’s use only for get operation.

How can I get to do something like this

thanx



Answer this question

Set hour, minute, second to Date object

  • Pela06

    Ooops, didn't see in what forum it was in.

    Moved to C# Express as it is more suitable here.



  • AlanW2

    DateTime NowTime = DateTime.Now;

    DateTime MyDate = new DateTime(NowTime.Year, NowTime.Month, NowTime.Date);

    There are other constructor overloads if you wish to specify more advanced features and you can read about them by clicking here


  • Coombsey

    This should do it.

    DateTime dt = DateTime.Now.Date;



  • Mark Goldstein

    Sorry, post on wrong place


  • AndyB1979

    This is not the right forum for general questions.

    That being set, to set the date/time to your custom date/time you can use this:

    ------------------------------------

    using System.Globalization;

    .....

    CultureInfo MyCultureInfo = new CultureInfo("de-DE");

    string MyString = "12 Juni 2004 4:00:01";

    DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo);

    Console.WriteLine(MyDateTime);

    ...

    ----------------------------------------------


  • Set hour, minute, second to Date object