Parse Date

  • Thread starter Thread starter Doug Bell
  • Start date Start date
D

Doug Bell

Hi,
I have some data in the format of yyyymmdd (Int32) that I want to display,
in a datagrid, formatted in International Date Format dd MM yyyy.

Any suggestions on an efficient approach?

Thanks,

Doug
 
Doug Bell said:
I have some data in the format of yyyymmdd (Int32) that I want
to display in a datagrid, formatted in International Date Format
dd MM yyyy.

Dim i as Integer _
= <value in form yyyymmdd>
Dim d as DateTime _
= CDate( i.ToString( "0000\-00\-00" ) )
DoSomethingWith( d.ToString( "dd MMM yyyy" ) )

HTH,
Phill W.
 
Thanks Phil, that is fine for taking a value from a single record and
converting it but I was looking for a way to display the whole datatable in
a datagrid.

Doug
 
Doug

What is the datasource if it is a table than you can maybe add an extra
table column.

Than you can use this code in a for loop to fill that extra column.

Dim intdat As Integer = 20050808 'creating your integer and trying to see if
I understood you.
Dim intString As String = intdat.ToString
Dim myDate As DateTime = DateTime.ParseExact(intString, "yyyyMMdd", Nothing)

However you are direct in trouble when you want to add a row because you
cannot set this in an expression.

I hope this helps,

Cor
 
Thanks Cor,
I was hoping to find a way to parse or format the field but adding a column
is an acceptable solution.

Doug
 
Doug,
In addition to the other comments. I would recommend adding a second column
of type DateTime to your data table.

You could try using an Expression Column that converts from an Integer into
a Date.
http://msdn.microsoft.com/library/d...fsystemdatadatacolumnclassexpressiontopic.asp

I would look at using modulus & division to split the int32 into year, month
& day, then use Convert & '+' to convert this into a string, and finally use
Convert to convert the string into a DateTime.

Seeing as how messy that formula might be, I would consider using a simple
for loop to update each row:


Something like (untested):
For Each row As DataRow in table.Rows
Dim yyyymmdd As Integer = DirectCast(row!yyyymmdd, Integer)
Dim yyyy As Integer = yyyymmdd \ 10000
Dim mm As Integer = (yyyymmdd \ 100) Mod 100
Dim dd As Integer = yyyymmdd Mod 100
row!theDateTime = New DateTime(yyyy, mm, dd)
Next

FWIW: I normally encapsulate the conversion from Integer to DateTime in its
own routine.

Alternatively I would consider using DataTable.Events to "compute" the
DateTime column, post if you want an example of this.

Hope this helps
Jay

| Hi,
| I have some data in the format of yyyymmdd (Int32) that I want to display,
| in a datagrid, formatted in International Date Format dd MM yyyy.
|
| Any suggestions on an efficient approach?
|
| Thanks,
|
| Doug
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top