1

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.

Ian
  • 30,182
  • 19
  • 69
  • 107
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216

3 Answers3

1

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.

MSDN link.

Ian
  • 30,182
  • 19
  • 69
  • 107
  • I want ddate/Month/Year as date format and you havent consider machines cultures – I Love Stackoverflow Jan 21 '16 at 05:39
  • @Learning In that case, then swap the day and month. And the culture needs to be settled by `CultureInfo`. But since you use `Datepicker1.SelectedDate.Value.ToShortDateString()`, then `"M/d/yyyy"` or `"d/M/yyyy"` for the `DateTime.ParseExact` would do. – Ian Jan 21 '16 at 05:44
  • i have tried this but still getting same error:DateTime dt = DateTime.ParseExact(Datepicker1.SelectedDate.Value.ToShortDateString(), "d/M/yyyy HH:mm:ss", null); – I Love Stackoverflow Jan 21 '16 at 05:45
  • @Learning check your `string` correctly in the debug mode, and then strictly follow the format of the `string`. Otherwise, use multiple formats as I suggested. – Ian Jan 21 '16 at 05:46
  • i have checked in debug mode and getting the same date as i have specified in my question – I Love Stackoverflow Jan 21 '16 at 05:49
  • You mean `Datepicker1.SelectedDate.Value.ToShortDateString()=1/21/2016`? and then you use `"M/d/yyyy"` and still get the same error? – Ian Jan 21 '16 at 05:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101246/discussion-between-learning-and-ian). – I Love Stackoverflow Jan 21 '16 at 05:51
1
@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

Dah Sra
  • 4,107
  • 3
  • 30
  • 69
1

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);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • i was expecting answer from you because when i was searching for this issue i have find your answer and have read it too and it was really good with good explaination and after reading it only i have manage to understand something about this culture – I Love Stackoverflow Jan 21 '16 at 11:28
  • @Learning Culture setting applies the rules that when you parsing a string to DateTime or generate string representation of a DateTime. – Soner Gönül Jan 21 '16 at 11:39
  • Actually the problem is my system date time is in month/date format and i am specifying date/month format that is why it is giving error – I Love Stackoverflow Jan 22 '16 at 04:05