Click here to Skip to main content
15,944,832 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm having a string like '30/03/2016 11:00 AM' i.e., 'dd/MM/YYYY hh:mm a' Format .

Please let me know how to convert this string to date time in C#.

Note: Datetime.Now giving me datetime in 'MM/dd/yyyy hh:mm a' format

What I have tried:

I tried

DateTime saveNow = DateTime.ParseExact(objCalendarFields.EventDateTime.ToString(), "tt", CultureInfo.InvariantCulture);

which giving me exception invalid date time
Updated 23-Jun-16 0:13am
Karthik_Mahalingam 23-Jun-16 5:55am    
what is the data type of EventDateTime
Member 12550264 23-Jun-16 6:09am    
Karthik_Mahalingam 23-Jun-16 6:13am    
check my solution.
Member 12550264 23-Jun-16 7:05am    

"Datetime.Now giving me datetime in 'MM/dd/yyyy hh:mm a' format"
No, it doesn't.
It gives you an DateTime value - which is a number of ticks since a specified moment in time. Your system is converting the DateTime value to a formatted string using the current system locale when you call ToString on it.
So instead, just use the value:
DateTime saveNow = objCalendarFields.EventDateTime;

DateTime saveNow = (DateTime)objCalendarFields.EventDateTime;

depending on the declaration of EventDateTime.
Share this answer
BillWoodruff 23-Jun-16 9:01am    
try this

DateTime dt = DateTime.ParseExact("30/03/2016 11:00 AM", "dd/MM/yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture);
Share this answer
Member 12550264 23-Jun-16 7:05am    
Thank You.
Karthik_Mahalingam 23-Jun-16 7:21am    
If your issue is resolved pls close this post
BillWoodruff 23-Jun-16 9:01am    
Member 12550264 24-Jun-16 3:49am    
giving me 7/3/2016 11:00 AM.

Required 07/03/2016 11:00 AM
Karthik_Mahalingam 24-Jun-16 4:12am    
where it is giving?
Please have a look at these links and if you are still having an issue then send a reply:

How to: Convert a String to a DateTime (C# Programming Guide)[^]

Easy String to DateTime, DateTime to String and Formatting[^][^][^]

Probably your formatting is incorrect.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900