Converting string to datetime

Posted by Nkkppp under C# on 1/22/2013 | Points: 10 | Views : 4356 | Status : [Member] | Replies : 5
Hi,

Is there a way to Convert MM/dd/yyyy string to datetime using C#.

I have tried various methods but the problem is when I convert the datetime format is

dd/MM/yyyy and not MM/dd/yyyy.

Can anyone please help.

Thanks.




Responses

Posted by: Satyapriyanayak on: 1/23/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Refer

http://www.csharp-examples.net/string-format-datetime/

If this post helps you mark it as answer
Thanks

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 1/23/2013 [Member] Bronze | Points: 25

Up
0
Down
public static DateTime changedate(string strdt)

{
string strdate = null;
int strdateday = 0;
int strdatemonth = 0;
int strdateyear = 0;
int intc = 0;
intc = strdt.IndexOf("/");
strdateday = Convert.ToInt32(strdt.Substring(0, intc));
strdt = strdt.Substring(intc + 1);
intc = strdt.IndexOf("/");
strdatemonth = Convert.ToInt32(strdt.Substring(0, intc));
strdt = strdt.Substring(intc + 1);
strdateyear = Convert.ToInt32(strdt);

strdate = strdatemonth + "/" + strdateday + "/" + strdateyear;
strdate = strdate.Trim();

return new DateTime(strdateyear, strdatemonth, strdateday);
}


This helps you with minmial modication.

Makr the answer if this helps you

Thanks
Pavan Kumar
Mark Answer if this fits the need

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 1/23/2013 [Member] [MVP] Gold | Points: 25

Up
0
Down
try this,

string DateString = "04/22/2013"; // MM/dd/yyyy
DateTime date = new DateTime();
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
// It will Return 04/22/2013 (MM/dd/yyyy)
date = DateTime.ParseExact(DateString, "MM/dd/yyyy", provider);
Response.Write(date); // OutPut : 04/22/2013
// It will Return 22/04/2013 (dd/MM/yyyy)
string Date = date.ToString("dd/MM/yyyy");
Response.Write("<br/>" + Date); // OutPut : 22/04/2013



Please Mark As Answer if this helps you

.

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kundan64 on: 1/23/2013 [Member] Starter | Points: 25

Up
0
Down
use
 DateTime.Parse()
method.

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Brianmanee on: 12/14/2016 [Member] Starter | Points: 25

Up
0
Down
try TryParse method


public static bool TryParse(
string s,
out DateTime result
)


This method is similar to the DateTime.Parse(String) method, except that the
TryParse(String, DateTime)
method does not throw an exception if the conversion fails. More about..

http://net-informations.com/q/faq/stringdate.html
Brian

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response