Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, May 21, 2012 9:17 AM
Hi,
I want to split date into parts. i.e. 1/1/2012 ==> month = 1; day = 1; and year = 2012 or 12. C# gives day name not nuber. we can get month pottion and year portion creationDate.Month; and creationDate.year; respectively.
Any one have idea???
Atiq
All replies (5)
Monday, May 21, 2012 9:25 AM ✅Answered
If you already have the date in a DateTime you can miss out the ParseExact;
string d = "1/1/2012";
DateTime dt = DateTime.ParseExact(d, "M/d/yyyy", System.Globalization.CultureInfo.CurrentCulture);
int day = dt.Day;
int month = dt.Month;
int year = dt.Year;
Tuesday, May 22, 2012 1:43 AM ✅Answered
Thanks All, I have got it by simple code;
DateTime creationDate = DateTime.Now;
int day = creationDate.Day;
int month = creationDate.Month;
int year = creationDate.Year;
Atiq
Monday, May 21, 2012 9:46 AM
thanks, but it genrates the fallowing exception;
String was not recognized as a valid DateTime.
Atiq
Monday, May 21, 2012 6:24 PM
it genrates the fallowing exception;
String was not recognized as a valid DateTime.
That would be because the string you have provided is not in a valid DateTime format accorsing to the conversion you are trying to apply. Fix you string or fix your conversion. Since you haven't shown any details we can't do anything better than to advise you to 'fix it'.
Monday, May 21, 2012 6:47 PM
@ Atiqiub
By date, do you mean the .NET DateTime Structure?
http://msdn.microsoft.com/en-us/library/system.datetime.aspx
The .NET DateTime Structure provides all the methods and properties that you require for most common uses:
http://msdn.microsoft.com/en-us/library/system.datetime.day.aspx
day of the month ... 1..31
http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek
day of the week ... enumerated constant that indicates the day of the week
http://msdn.microsoft.com/en-us/library/system.datetime.dayofyear
day of the year ... expressed as a value between 1 and 366
For day of the month, use the .Day property
g.