3

I am getting a date and time from a data feed which I cannot change, and I need to convert it to a DateTime.

The format of the string is as follows.

timestamp="20131204T171054+0000"

4 Answers 4

5

You could use DateTime.ParseExact and specify a Custom Date and Time Format String.

Your sample looks like "yyyyMMddTHHmmssK" might work.

In vb.net

Dim timestamp As String = "20131204T171054+0000"
Dim dt As DateTime = DateTime.ParseExact(timestamp, "yyyyMMddTHHmmssK", CultureInfo.InvariantCulture)

In C#

string timestamp = "20131204T171054+0000";
DateTime dt = DateTime.ParseExact(timestamp, "yyyyMMddTHHmmssK", CultureInfo.InvariantCulture);
Sign up to request clarification or add additional context in comments.

Comments

1

I'd take Haji's approach if you are dealing with a well defined environment. Global time handling is a bear.

I'd split the string to a valid date and time:

Dim dt As DateTime = "2013.12.04 17:10:54"

then adjust for the offset.

Comments

0

I have had to resort to using sub strings here is my code:

Dim Date As Date = datestamp.Substring(6, 2) & "/" & datestamp.Substring(4, 2) & "/" & datestamp.Substring(0, 4) & " " &
                            timestamp.Substring(0, 2) & ":" & timestamp.Substring(2, 2) & ":" & timestamp.Substring(4, 2)

Comments

-1

use the DateTime.Parse Function. You must pass a string value as its argument. Then, you can assign a DateTime to its result. This transforms the string into a value indicating the DateTime. below is the example of converting the sample string to datatime. I don't have any idea about the string you given in the question..

  Dim value As String = "2000-02-02"
  Dim time As DateTime = DateTime.Parse(value)

2 Comments

What that has to do with specific datetime format mentioned by OP?
@YuriyGalanter sorry my mistake. as per the question header i given some string for sample conversion.

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.