Reputation: 6868
I am taking selected date from telerik date picker and want to take that selected date and current system time by 24 hours format and want date like this:
For Ex: Current Date = dd/mm/yy HH:Minutes:Seconds
21/1/2016 14:48:21
This is my code:
DateTime dt = DateTime.ParseExact(Datepicker1.SelectedDate.Value.ToShortDateString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);//Error:String was not recognized as a valid DateTime.
Datepicker1.SelectedDate.Value= {1/21/2016 12:00:00 AM}
Datepicker1.SelectedDate.Value.ToShortDateString()=1/21/2016
Error:String was not recognized as a valid DateTime on Datetime.ParseExact
.
Upvotes: 1
Views: 251
Reputation: 98850
You don't need to parse anything.
Your Datepicker1.SelectedDate.Value
is already DateTime
, just assign this value to your dt
variable.
DateTime dt = Datepicker1.SelectedDate.Value;
If you want to get it's string representation based on your format, just use ToString
method with proper format.
string formattedDate = dt.ToString("dd/M/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
which returns 21/1/2016 14:48:21
as a string
.
Also if you want 1/21/2016
as a string, just change your format to M/dd/yyyy
like;
string formattedDate = dt.ToString("M/dd/yyyy", CultureInfo.InvariantCulture);
Upvotes: 1
Reputation: 30813
Change your format in the ParseExact
from
"dd/MM/yyyy"
to
"M/d/yyyy" or "M/d/yyyy h:m:s tt" //the first one use .ToShortDateString(), the second one for 1/21/2016 12:00:00 AM
The first one only takes care for case like 21/12/1997
The second one takes care 12/21/1997
, 2/21/1997
, 12/2/1997
, and 2/1/1997
in addition to take care of time info
Also, note that you may consider to have multiple formats (just in case): "d/M/yyyy H:m:s"
, "d/M/yyyy h:m:s tt"
to take care of the case where day and month are swapped and when you have AM/PM
.
Upvotes: 1
Reputation: 4445
@yourDateTime.FormattedReviewDate.ToString("MMM dd,yyyy")
You might even just add a simple property to dateTime for the formatted display:
public string FormattedReviewDate
{
get { return ReviewDate.ToString("MMM dd,yyyy"); }
}
Note: Give your needed format
Upvotes: 1