Вопрос

How to convert date from one form to another? I have a string variable bid="3/11/2014 10:57:00 PM". Where 3=month,11=day,2014=year,10=hour in 12 hour format, 57=minute,00=seconds PM=AM/PM format.

I need to convert variable bid into this date format and store it in a date variable. Bid has to be converted in this format: "11/3/2014 10:57:00 PM" where 11=day,3=month,2014=year 10=hour in 12 hour format,57=minute,00=seconds,PM=AM/PM format.

I tried Datetime.parse exact, with several combinations of strings but not worked. Can someone please help me out?

Это было полезно?

Решение

I need to convert variable bid into this date format and store it in a date variable

This works for me:

var dt = DateTime.ParseExact("3/11/2014 10:57:00 PM", 
                             "M/d/yyyy h:mm:ss tt",
                             CultureInfo.InvariantCulture)

Bid has to be converted in this format: "11/3/2014 10:57:00 PM" where 11=day,3=month,2014=year 10=hour in 12 hour format,57=minute,00=seconds,PM=AM/PM format.

Well, you could convert it back to a stirng:

var s = dt.ToString("d/M/yyyy h:mm:ss tt");

but why do you need it in that format? If you're sending it to a database then just leave it as a date - the database will then not have to worry about the date being in a specific format. If you're displaying it in an app then either use the user's culture or define the format in the UI layer.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top