Вопрос

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"
Это было полезно?

Решение 3

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)

Другие советы

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);

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.

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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top