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?
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
George GeorgePosted Jun 13, 2008, 5:01 AM
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
Ryan AlfordPosted Jun 12, 2008, 11:34 AM
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 GeorgePosted Jun 12, 2008, 1:35 AM
Any comments to my above reply to Ryan?
regards,
George
mavericPosted Jun 12, 2008, 1:22 AM
George GeorgePosted Jun 11, 2008, 9:54 PM
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
Ryan AlfordPosted Jun 11, 2008, 11:47 AM
George GeorgePosted Jun 11, 2008, 3:50 AM
Thanks Ryan,
Is it a C# language Spec rule? Any property of a static property is static?
regards,
George
Ryan AlfordPosted Jun 10, 2008, 1:09 PM
George GeorgePosted Jun 9, 2008, 10:48 PM
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 AlfordPosted Jun 9, 2008, 9:55 AM
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 GeorgePosted Jun 9, 2008, 5:06 AM
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 AlfordPosted Jun 6, 2008, 9:27 AM
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 GeorgePosted Jun 6, 2008, 3:19 AM
Thanks nilanka dharmadasa,
Your solution works.
regards,
George
Nilanka DharmadasaPosted Jun 6, 2008, 2:40 AM
Or it can be done in one line like this.
DateTime
yesterdaytime = DateTime.Now.AddDays(-1);George GeorgePosted Jun 6, 2008, 12:26 AM
Thanks, man!
Question answered.
regards,
George
Jan MontanoPosted Jun 5, 2008, 11:57 PM
if (date1.Date != date2.Date)
George GeorgePosted Jun 5, 2008, 10:22 PM
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 MontanoPosted Jun 5, 2008, 10:11 PM
TimeSpan oneDay = new TimeSpan(1,0,0,0);
DateTime yesterday = DateTime.Now.Subtract(oneDay);