tags:

views:

82

answers:

4

Hi my string is as follows

        string s ="20000101";

I would like to convert to Date format. How can i

A: 

If C#/.NET, use DateTime.Parse. If Java, use DateFormat.parse

Randolpho
NOTE: I'm assuming C# because of the lower-case `string` type. If it's another language, please edit your question accordingly.
Randolpho
in c# only i am doing
Dorababu
A: 

The power of Google. There are a thousand articles and results on Google.

http://www.codeproject.com/KB/cs/String2DateTime.aspx

Putting even the minimum amount of effort into this problem would give you the answer. You must learn the skill of working things out for yourself, especially when the problem is so simple.

Kieren Johnstone
-1: No need to be mean here.
Brian Driscoll
@Brian: I don't think this is necessarily being mean. He's pretty accurate in what he says. I have a couple of co-workers who ask questions of this level without ever touching a search engine. Though I will agree with you on one point: if it's so annoying to have this question asked, then why bother answering it?
Joel Etherton
Hi Brian: That was mean? Every sentence is true, and to the point. It might be a reality check, this question is asking for someone else to 'learn for them'. It's surely better to point out that they can work it out by themself than feed them the answer. I'm all for keeping SO a question-and-answer site, but in software, as every discipline, shifting your perspective on a bad problem is better than solving it; i.e., 'learn to learn' was my advice. My answer = they may take offence but next time Google first. Actual answer = new question posted tomorrow 'how to convert integer to string?'.
Kieren Johnstone
+2  A: 

Assuming you are using C# and .Net you will want to use DateTime.ParseExact or DateTime.TryParseExact. The format string is most likely "yyyyMMdd".

var datestring = "20000101";

var date1 = DateTime.ParseExact(datestring, "yyyyMMdd", null);

// ... or ...

DateTime dateResult;
if (!DateTime.TryParseExact(datestring, "yyyyMMdd", 
                            null, DateTimeStyles.AssumeLocal, 
                            out dateResult))
    dateResult = DateTime.MinValue; //handle failed conversion here
Matthew Whited
Hi i am unable to pares it can u give me the exact code
Dorababu
@Dorababu, added two examples.
Matthew Whited
But i am getting in the format dd/mm/yy and i need not want timestamp
Dorababu
Is that your local standard for formatting? You can use the .ToString() from the datetime value to change the output format.
Matthew Whited
see this link for DateTime formatting => http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Matthew Whited
A: 

in C/C++, use the time.h (ctime) library's gmtime function, after converting the time to an integer: tm =gmtime(atoi(time_string));

dublev