Hello everyone,
I want to get the same time in yesterday, is there any bug in my below code?
My concern is code, _CurrentDate.Now.Day - 1, if current date is 1, 1 minus 1 is zero, not a valid date?
Code
DateTime _CurrentDate = DateTime.Now;
DateTime _LastDate = new DateTime(_CurrentDate.Now.Year, _CurrentDate.Now.Month, _CurrentDate.Now.Day - 1, _CurrentDate.Now.Hour, _CurrentDate.Now.Second);
thanks in advance,
George

Jan MontanoRank #1,295 · 1.3K reputationPosted Jun 5, 2008, 6:11 PM EDT
TimeSpan oneDay = new TimeSpan(1,0,0,0);
DateTime yesterday = DateTime.Now.Subtract(oneDay);
George GeorgeRank #1,736 · 870 reputationPosted Jun 5, 2008, 6:22 PM EDT
Cool, Jan!
A harder question for you, how to check if two DateTime type instance is in the same date?
(example, 8am and 9am of yesterday belong to the same date, but 11:30pm from yesterday to 0:30am today, even if just elapse one hour, they do not belong to the same date.)
regards,
George
Jan MontanoRank #1,295 · 1.3K reputationPosted Jun 5, 2008, 7:57 PM EDT
if (date1.Date != date2.Date)
George GeorgeRank #1,736 · 870 reputationPosted Jun 5, 2008, 8:26 PM EDT
Thanks, man!
Question answered.
regards,
George
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Jun 5, 2008, 10:40 PM EDT
Or it can be done in one line like this.
DateTime
yesterdaytime = DateTime.Now.AddDays(-1);George GeorgeRank #1,736 · 870 reputationPosted Jun 5, 2008, 11:19 PM EDT
Thanks nilanka dharmadasa,
Your solution works.
regards,
George
Ryan AlfordRank #1,411 · 1.2K reputationPosted Jun 6, 2008, 5:27 AM EDT
so your, "_currentDate.Now.Year", would have thrown exceptions.
I am still wondering why you asked your first question. a simple copy/paste into Visual Studio, then running(after fixing the .Now errors), it would have thrown an error saying that the month, day, year parameters are not a valid date.
Please, download Visual Studio 2008 Express Edition. IT IS FREE. Then take some of this code, and TEST IT YOURSELF.
George GeorgeRank #1,736 · 870 reputationPosted Jun 9, 2008, 1:06 AM EDT
Thanks Ryan,
I did some tests and maybe my testing is not enough.
1.
You mentioned -- "and since it's obvious you didn't write the original post's code in Visual Studio, when you create an instance of DateTime, you do not have access to the ".Now" property." could you say in other words please? What means "do not have access to ..."?
2.
"so your, "_currentDate.Now.Year", would have thrown exceptions." -- which statement do you mean will generate exceptions? I have tested again in Visual Studio 2008, it runs ok. Please feel free to point my mistake.
regards,
George
Ryan AlfordRank #1,411 · 1.2K reputationPosted Jun 9, 2008, 5:55 AM EDT
int year = _CurrentDate.Now.Year;
the reason is that ".Now.Year" is a static member of the DateTime class which means it can't be accessed using an instace of the DateTime class(_CurrentDate is an instance of the DateTime class).
therefore:
DateTime _LastDate = new DateTime(_CurrentDate.Now.Year, _CurrentDate.Now.Month, _CurrentDate.Now.Day - 1, _CurrentDate.Now.Hour, _CurrentDate.Now.Second);
will throw the error:
"Static member 'System.DateTime.Now.get' cannot be accessed with an instance reference; qualify it with a type name instead" for everything in bold.
George GeorgeRank #1,736 · 870 reputationPosted Jun 9, 2008, 6:48 PM EDT
Thanks Ryan,
You are correct. I have checked DateTime.Now is static, but whether DateTime.Now.Year is static or not? Why?
regards,
George
Ryan AlfordRank #1,411 · 1.2K reputationPosted Jun 10, 2008, 9:09 AM EDT
George GeorgeRank #1,736 · 870 reputationPosted Jun 10, 2008, 11:50 PM EDT
Thanks Ryan,
Is it a C# language Spec rule? Any property of a static property is static?
regards,
George
Ryan AlfordRank #1,411 · 1.2K reputationPosted Jun 11, 2008, 7:47 AM EDT
George GeorgeRank #1,736 · 870 reputationPosted Jun 11, 2008, 5:54 PM EDT
Thanks Ryan!
I have checked some internal code. Now is static property and Year is not static property. I think the operation DateTime.Now.Year performs the following opeations,
1. DateTime.Now will create a new temporary instance;
2. Year is called/retrieved on the new temporary instance.
Correct?
regards,
George
maveric8 reputationPosted Jun 11, 2008, 9:22 PM EDT
George GeorgeRank #1,736 · 870 reputationPosted Jun 11, 2008, 9:35 PM EDT
Any comments to my above reply to Ryan?
regards,
George
Ryan AlfordRank #1,411 · 1.2K reputationPosted Jun 12, 2008, 7:34 AM EDT
There are two ways to get the current year:
int year = DateTime.Now.Year;
_______________________________________
DateTime currentDate = DateTime.Now;
int year = currentDate.Year;
As you can see, I did NOT do this:
DateTime currentDate = DateTime.Now;
int year = currentDate.Now.Year;
The reason is because ".Now.Year" is not a valid property for a instance of the DateTime object.
George GeorgeRank #1,736 · 870 reputationPosted Jun 13, 2008, 1:01 AM EDT
Thanks Ryan!
I agree with you. But my question is, what operations are performned when DateTime.Now.Year is executed. I think the following 2 operations are performed, correct?
1. DateTime.Now will create a new temporary instance;
2. Year non-static property is called/retrieved on the new temporary instance.
regards,
George