C# .NET - Conversion of String To DateTime Not Working -------

Asked By suresh krishna on 29-Jun-12 11:59 AM


I want to convert string 'mm/dd/yyyy' to datetime in c sharp but my system date format in UK ?


How to resolve? Please help me out quickly?

With Thanks,
Suresh .K
Robbe Morris replied to suresh krishna on 29-Jun-12 12:14 PM
CultureInfo class.  It is a parameter to .ToString() off a DateTime class.
Super Man replied to suresh krishna on 29-Jun-12 12:36 PM
DateTime dt = DateTime.ParseExact(datestring, "mm/dd/yyyy",CultureInfo.InvariantCulture);
suresh krishna replied to Super Man on 30-Jun-12 12:08 AM
Hi Super Man.. Its works for me .. But the given date converted as different date.

like '13/06/2012' converted as '06/01/2012 13:00' . Please suggest me on this
suresh krishna replied to Super Man on 30-Jun-12 12:08 AM
Hi Super Man.. Its works for me .. But the given date converted as different date.

like '13/06/2012' converted as '06/01/2012 13:00' . Please suggest me on this
suresh krishna replied to Super Man on 30-Jun-12 12:08 AM
Hi Super Man.. Its works for me .. But the given date converted as different date.

like '13/06/2012' converted as '06/01/2012 13:00' . Please suggest me on this
Super Man replied to suresh krishna on 30-Jun-12 12:38 AM
DateTime dt = DateTime.ParseExact(datestring, "MM/dd/yyyy",CultureInfo.InvariantCulture);


mm for minute
MM for month.
suresh krishna replied to Super Man on 30-Jun-12 01:02 AM
Hi Super Man .. My System Culture is UK format..

How can I convert the date as MM/dd/yyyyy. 

Chintan Vaghela replied to suresh krishna on 30-Jun-12 01:32 AM

Hi Frndz,

 

Functionality:  Set UK Culture Date format dd/mm/yyyy

 

 

To achieve this task,

 

First set Culture

 

CultureInfo MyUsersCulture = Thread.CurrentThread.CurrentCulture;

 

        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

 

After then Parse to Date

 

   DateTime dtTemp = DateTime.ParseExact(dateString, "dd/MM/yyyy", null);

 

Full Logic     :

 

 

        CultureInfo MyUsersCulture = Thread.CurrentThread.CurrentCulture;

        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

        string dateString = "30/12/2012";

        // in order not to have to specify a FormatProvider

        DateTime dtTemp = DateTime.ParseExact(dateString, "dd/MM/yyyy", null);

 

 

 

Hope this helpful!

Thanks

 

 

 

Lalit M replied to suresh krishna on 30-Jun-12 02:51 AM
To convert a string-based date to a System.DateTime object, you can use the Convert.ToDateTime(String) method or the DateTime.Parse(String) static method.

read here different format for Conversion of String To DateTime:-> http://www.csharp-examples.net/string-format-datetime/