DateTime syntax help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey all,
i'm just trying to test datetime functions:

DateTime dt1 = "9/15/2007";
DateTime dt2 = "9/17/2007";
DateTime dtResult;

dtResult = dt2 - dt1;

The above is what i'm trying to say but, of course my syntax is way off. can
someone please help me with this syntax?

thanks,
rodchar
 
hey all,
i'm just trying to test datetime functions:

DateTime dt1 = "9/15/2007";
DateTime dt2 = "9/17/2007";
DateTime dtResult;

dtResult = dt2 - dt1;

The above is what i'm trying to say but, of course my syntax is way off. can
someone please help me with this syntax?

thanks,
rodchar

You probably want something along the lines of:
DateTime dt1 = DateTime.Parse("9/15/2007");
DateTime dt2 = DateTime.Parse("9/17/2007");

TimeSpan dtdiff = dt2 - dt1;

TimeSpan has several methods and properties you can use to determine
how many days/hours/minutes/seconds are in dtdiff.
 
Just to clairify, you have to do something like this in your code, as C#
does not support a literal for date time values.

Because of this, if your code is going to be compiled on other machines
that do not have the same locale settings, parsing a string is a bad idea,
and you should use the constructor for the structure that takes years,
months, days, etc, etc.
 
could you give a small example of using the constructor in that way?

Nicholas Paldino said:
Just to clairify, you have to do something like this in your code, as C#
does not support a literal for date time values.

Because of this, if your code is going to be compiled on other machines
that do not have the same locale settings, parsing a string is a bad idea,
and you should use the constructor for the structure that takes years,
months, days, etc, etc.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Arnshea said:
You probably want something along the lines of:
DateTime dt1 = DateTime.Parse("9/15/2007");
DateTime dt2 = DateTime.Parse("9/17/2007");

TimeSpan dtdiff = dt2 - dt1;

TimeSpan has several methods and properties you can use to determine
how many days/hours/minutes/seconds are in dtdiff.
 
could you give a small example of using the constructor in that way?

DateTime dt = new DateTime(2005, 9, 17);

Which bit were you having difficulty with?

Jon
 
// Corresponds to today, 9/17/2007.
DateTime dt = new DateTime(2007, 9, 17);


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

rodchar said:
could you give a small example of using the constructor in that way?

Nicholas Paldino said:
Just to clairify, you have to do something like this in your code, as
C#
does not support a literal for date time values.

Because of this, if your code is going to be compiled on other
machines
that do not have the same locale settings, parsing a string is a bad
idea,
and you should use the constructor for the structure that takes years,
months, days, etc, etc.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Arnshea said:
On Sep 17, 10:24 am, rodchar <[email protected]>
wrote:
hey all,
i'm just trying to test datetime functions:

DateTime dt1 = "9/15/2007";
DateTime dt2 = "9/17/2007";
DateTime dtResult;

dtResult = dt2 - dt1;

The above is what i'm trying to say but, of course my syntax is way
off.
can
someone please help me with this syntax?

thanks,
rodchar

You probably want something along the lines of:
DateTime dt1 = DateTime.Parse("9/15/2007");
DateTime dt2 = DateTime.Parse("9/17/2007");

TimeSpan dtdiff = dt2 - dt1;

TimeSpan has several methods and properties you can use to determine
how many days/hours/minutes/seconds are in dtdiff.
 
Back
Top