Share via


Creating new datetime from string with AM / PM and Filtering

Question

Tuesday, October 7, 2014 2:56 PM

Hi,

I have a date stored in DB in string format 10/6/2014 4:53:04 PM as 10062014045304PM. How to create a new date from this and filter records in the linq where class.

My goal is to filter records those are greater than the provided date values.

All replies (4)

Wednesday, October 8, 2014 9:00 AM âś…Answered

Hi friend,

I would suggest you to check the following thread. It incldes AM or PM format "tt"

Convert the property value to yyyy-MM-dd hh:mm:ss tt format 

Best regards,

Kristin

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Tuesday, October 7, 2014 3:17 PM

Hi,

The System.DateTime class provides support for handling and working with dates. This class also provides some convert/parse methods to get the Date(Time) from a string. There are a couple of ways to do that :

/// The normal method
DateTime convertedDate = DateTime.Parse(yourString);

/// The method that is for regional dates
DateTime convertedDate = DateTime.ParseExact(yourString);

/// Same as the first method but this method tries to
/// convert it, if it fails in will continue because of
/// the try concept
DateTime convertedDate = DateTime.TryParse(yourString);

/// Same as the second method but this method tries to
/// convert it, if it fails in will continue because of
/// the try concept
DateTime convertedDate = DateTime.TryParseExact(yourString)

I hope that answered your questions, if not please do ask for help :)

Regards Bram Sinke


Tuesday, October 7, 2014 3:24 PM

Thanks.

 DateTime date; ///4:53:04 PM   H:mm:ss ttDateTime.TryParseExact("10062014045304", "MMddyyyyHHmmss", CultureInfo.InvariantCulture, 
                System.Globalization.DateTimeStyles.None, out date);For me this works but when i include AM or PM and in the format "tt". I don't get correct date format
            

Friday, October 10, 2014 2:42 PM

Hey,

Sorry for my late response :P

I think Kristin has the answer for you! If her post solved your problem, then can please mark Kristin's post as the solution. Then other people can also find this post and use it to solve there problem. Thanks in advance

Regards Bram