DateTime syntax help

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
 
A

Arnshea

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

Guest

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.
 
J

Jon Skeet [C# MVP]

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
 
N

Nicholas Paldino [.NET/C# MVP]

// 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top