2

I am getting this datetime format in an xml file:

2012-06-14T11:15:41.587-07:00
2012-06-14T10:49:32.397-07:00
2012-06-11T11:10:49.2-07:00

I believe I understand some of it, please correct me if I am wrong:

2012-06-14 = date

T = time identifier

10:49:32.397 = hour min second milliseconds

-07:00 = I have no idea

I need to convert this to something I can sort on for my datagrid view.

When I try something like this:

Console.WriteLine(String.Format("{0:d/M/yyyy HH:mm:ss}", "2012-06-14T10:49:32.397-07:00"))

I'm getting the original string back out with no conversion.

Anyone have any suggestions?

0

3 Answers 3

4

-07:00 = I have no idea

-7 is a timezone offset. It means the DateTime is 7 hours behind UTC, which would indicate the US Mountain Time.

You want to use DateTime.Parse to get a DateTime object.

Dim val As String = "2012-06-11T11:10:49.2-07:00"
Dim parsedDateTime As DateTime = DateTime.Parse(val)
'Do whatever with parsedDateTime here
Sign up to request clarification or add additional context in comments.

4 Comments

worked perfect although how do I get this as a DefaultCellStyle.Format for a datagridview column though? Do I need to convert each cell's value? I know I didn't clarify that in the question.
@ErocM That sounds like a different (new) question. I assume you are talking about DataGridView, which I don't have a lot of background in.
k you've got me started, I'll post a new question. Tyvm!!
If you are using a datagridview and you know the columsn that will be present at run time, you can set the columns up in the designer. Using VS 2008 when you select the datagridview a little arrow will show up on the top right. click that then an item on that menu will be Add Columns and Edit Columns. you can use the screens that those bring up to add all the columns you want and assign them datatypes and formats in there, some of the attributes are a little buried but just poke around a bit and you'll find what you need. (character limit is cutting me off, sorry)
0

Take a look at this article:

http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

It should give you a better grasp of how to use the DateTime object. The time you have is the ISO 8601 format.

Comments

0

The -07:00 is the offset from UTC. You can parse this via DateTimeOffset.ParseExact, which includes a specifier for the offset ("zzz" format specifier).

In your case, I believe this would be :

Dim xmlValue = "2012-06-14T11:15:41.587-07:00"

Dim value as DateTimeOffset = DateTimeOffset.ParseExact(xmlValue, "yyyy-MM-dd\Thh:mm:ss.fffzzz", CultureInfo.InvariantCulture)

1 Comment

The standard Parse method knows how to parse ISO 8601 dates, but this works too.

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.