3

I have a date time string "12-24-2013 15:19:29" which is in "MM-dd-yyyy HH:mm:ss". I want to convert this string to datetime. But the format should not change.ie, it should be the same "MM-dd-yyyy HH:mm:ss" format.

When I used following method,

DateTime dt2 = DateTime.ParseExact(date31strin, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);

the format is changed to "dd-MM-yyyy HH:mm:ss". I have tried some other method also, but still getting this same format.

1
  • A DateTime is just a number, the format for the to string can be set to whatever you want. Commented Dec 24, 2013 at 16:19

5 Answers 5

7

First, you should be parsing the string to a DateTime and then format it to the second format using ToString() method.

//Convert your string to a DateTime
DateTime dt2 = DateTime.ParseExact(date31strin, 
                        "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);


//If you want to change the format
string secondFormat = dt2.ToString("Second format string");

Note: Date is a Date and it does not have a format. If you need to convert the string to a DateTime, first line of code is enough

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, but here secondFormat is string. I want it to be datetime format.
Even if you see Date in a certain format, Date actually does not have a format. Format you are seeing may depend of the culture setting of the computer.
I need to pass this date to a webservice, and that should be in datetime, not string. Thats why i need it ito be converted to datetime
If your webservice expecting a DateTime type, your code should be okay. dt2 is a DateTime type.
Yes, dt2 is a DateTime. But in your code, its format is "MM/dd/yyyy HH:mm:ss". The webservice is expecting a DateTime type, and it should be in "MM-dd-yyyy HH:mm:ss". that is what my problem. Really sorry, in my question its "MM/dd/yyyy HH:mm:ss". never mind, I have changed it :) ..
|
2

You are not changing the format. You are parsing a string into a DateTime object which does not have a format.

When you decide to present the DateTime, you can format it any way you wish.

2 Comments

Can u please tell me how can I do it? I am still a beginner :)
Use the ToString method with formatters: msdn.microsoft.com/en-us/library/az4se3k1.aspx
0

to parse String to DateTime use method DateTime.TryParse (http://msdn.microsoft.com/cs-cz/library/ch92fbc1%28v=vs.110%29.aspx) and then use DateTime formating (http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx) to output to aspx page

Comments

0

//frndzz if you are using j query and you don't convert date in(MM/dd/yyyy) to //(dd/MM/yyyy) then this code will help you // i try it and i get the answer

string sd=txtmydate.Text //sd=04/27/2015 (MM-dd-yyyy) format string [] sdate = sd.Split('-');

      string fd = sdate[1];
      fd=string.Concat(sdate[1],"-",sdate[0],"-",sdate[2]);
      DateTime dt = Convert.ToDateTime(fd);

      txtshow.Text = dt.ToString("dd/MM/yyyy");

        //txtshow will show you date as 27-04-2015  (dd/MM/yyyy) format

thanks

Comments

0

How to convert string to DateTime:

string iDate = "Absent";
aEmployeeAttendence.Intime = DateTime.Parse(iDate);  
//aEmployeeAttendence (object)
//Intime (field)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.