a question regarding DateTime

J

Jeff

Hey

..NET 2.0

I'm about to create 2 input parameters for a method. These are 2 DateTime
parameters - one named "from" and another named "to"... - from and to
DateTime values...

Okay the trick is that the "from" DateTime should be like this
01.<MM>.<YYYY> ss:mm.hh which means that today is the 03.12.2007 then the
"from" date should be 01.12.2007 00:00:00. What is the best way of
calculating this?

any suggestions?
 
J

Jon Skeet [C# MVP]

Jeff said:
.NET 2.0

I'm about to create 2 input parameters for a method. These are 2 DateTime
parameters - one named "from" and another named "to"... - from and to
DateTime values...

Okay the trick is that the "from" DateTime should be like this
01.<MM>.<YYYY> ss:mm.hh which means that today is the 03.12.2007 then the
"from" date should be 01.12.2007 00:00:00. What is the best way of
calculating this?

DateTime values don't have any inherent formatting - it's only when you
convert them to and from strings that they're formatted.
 
J

Jeff

Okay, I've testing this solution now:

DateTime from
from = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0)

I'm debugging the code and it seems like it is working. Do you see any
problems using this code?.
 
J

Jon Skeet [C# MVP]

Jeff said:
Okay, I've testing this solution now:

DateTime from
from = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0)

I'm debugging the code and it seems like it is working. Do you see any
problems using this code?.

Yes - you're calling DateTime.Now twice. I know it's unlikely, but
consider the case where it's December 31st, 23:59:59.999 - you call
DateTime.Now and the year is 2007. Then the clock times, and then you
call DateTime.Now and the month is January. Now you neither have Dec
2007 nor Jan 2008 - you have Jan 2007.

Use

DateTime now = DateTime.Now;
from = new DateTime(now.Year, now.Month, 1, 0, 0, 0)

(Are you sure you don't want to be using UTC, by the way? I personally
try to keep to UTC whenever I can until the presentation layer, but I
don't know enough about what you're doing to say for sure. Just
something to think about.)
 
J

Jeff

What about this approach:

DateTime date = DateTime.Now;
DateTime from
from = new DateTime(date.Year, date.Month, 1, 0, 0, 0)
 
J

Jon Skeet [C# MVP]

Jeff said:
What about this approach:

DateTime date = DateTime.Now;
DateTime from
from = new DateTime(date.Year, date.Month, 1, 0, 0, 0)

Isn't that exactly what I put, just having changed the name of the
variable from "now" to "date"?
 
P

Peter Duniho

Isn't that exactly what I put, just having changed the name of the
variable from "now" to "date"?

Logically, sure. But he fixed one of your errors (missing variable
declaration) and introduced a new instance of the other one (missing
semi-colon). Surely that counts for something. :) :)
 
L

Liz

Jeff said:
Hey

.NET 2.0

I'm about to create 2 input parameters for a method. These are 2 DateTime
parameters - one named "from" and another named "to"... - from and to
DateTime values...

Okay the trick is that the "from" DateTime should be like this
01.<MM>.<YYYY> ss:mm.hh which means that today is the 03.12.2007 then the
"from" date should be 01.12.2007 00:00:00. What is the best way of
calculating this?

any suggestions?

so you want the current month.year, always prefixed with "01" and postfixed
with "00:00:00" .. ? this should work:

DateTime dt = DateTime.Now;
string t = "01." + dt.Month.ToString().PadLeft(2, '0') + "." +
dt.Year.ToString() + " 00:00:00";
DateTime dt2 = Convert.ToDateTime(t);
don't know if it's the "best" way ... it's a way ...
 
L

Lasse Vågsæther Karlsen

Liz said:
so you want the current month.year, always prefixed with "01" and postfixed
with "00:00:00" .. ? this should work:

DateTime dt = DateTime.Now;
string t = "01." + dt.Month.ToString().PadLeft(2, '0') + "." +
dt.Year.ToString() + " 00:00:00";
DateTime dt2 = Convert.ToDateTime(t);
don't know if it's the "best" way ... it's a way ...

That's what I call a solutionproblem, a solution with more problems than
it solves.

Go with the way that Jon posted.
 
L

Liz

Lasse Vågsæther Karlsen said:
Liz wrote:

That's what I call a solutionproblem, a solution with more problems than
it solves.

Go with the way that Jon posted.

I had not seen Jon's solution; now that I have, no doubt it's cleaner and I
would use it; compared to his, I see awkward, not problematic .. it does
work ... what "problems" do you see with this one other than it's slower and
more verbose?
 
J

J.B. Moreno

Liz said:
I had not seen Jon's solution; now that I have, no doubt it's cleaner and I
would use it; compared to his, I see awkward, not problematic .. it does
work ... what "problems" do you see with this one other than it's slower and
more verbose?

Cultural info would be my guess...plus unnecessary code (there's no
reason to do the padding or add the time part).


If you're going to do a string conversion, then...

DateTime dt2 = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/01"));

But Jon's method (using new DateTime) is better as it avoids string
parsing entirely, and is thus more readable and probably faster.
 

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