How to initial the datetime value to "01/01/2005 00:00:00"?

  • Thread starter Thread starter ABC
  • Start date Start date
You can do this manually like below in JS..


Of course you can do the same thing in cSharp DateTime class.



var now=new Date();
var wanted=format(now);


function format(now)
{
return
now.getDay()+"/"+now.getMonth()+"/"+now.getFullYear(); // you can add
now.getHours or getMinutes...


}
 
ABC said:
How to initial the datetime variable as value to "01/01/2005 00:00:00"?

You can use the constructor that allows you to specify each of the values:

[C#]
public DateTime(
int year,
int month,
int day,
int hour,
int minute,
int second
);

so you would say:

DateTime dt = new DateTime(2005, 1, 1, 0, 0, 0);

-mdb
 
Brian Gideon said:
DateTime.Parse("01/01/2005 00:00:00")

I'd advise against this solution. It's very inefficient to use string
parsing when you can specify the parts explicitly with a constructor.
The above might also fail in some cultures (although I don't know of
any where it would fail offhand).

I'd suggest using:

DateTime dt = new DateTime (2005, 1, 1);
 
Yep. I don't disagree with that suggestion, but since it was already
mentioned I thought I'd provide an alternate method that might be
helpful if the date was stored as a string which I thought, based on
the subject, was the intent of the OP :)

Brian
 
Back
Top