Solved

VB.NET change date from US format

Posted on 2015-01-07
6
255 Views
Last Modified: 2015-01-09
Hi guys, I have an unbound datagrid with 3 columns. The datagrid gets filled running a wmic command which pulls the list of installed updates on the machine. The third column contains the "installed on" date but it's in US date format (MM/dd/yyyy). How do I change it so it's "dd/MM/yyyy"?
0
Comment
Question by:amaru96
  • 2
  • 2
  • 2
6 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 40537023
what are your regional settings for a short datetime (date) ? Is your country set as US ?
I get the same if I do a "wmic qfe get" .. Maybe someone else can help you format the date but we probably would need a short code sample as to how you are populating the grid
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40537031
Hi amaru96;

Try something like the following.

DGV.Columns("CN").DefaultCellStyle.Format = "DF"

Where DVG = DataGridView instance name
      CN = The Column Name of the column to be formatted
      DF = Date Formatting String such as "dd/MM/yyyy"

Open in new window

0
 
LVL 1

Author Comment

by:amaru96
ID: 40537036
I have tried:

DataGridView1.Columns(2).DefaultCellStyle.Format = "dd/MM/yyyy"

No effect

I've also tried:
 Dim xRowCount As Integer = 0
        Do Until xRowCount = DataGridView1.RowCount
            DataGridView1.Rows(xRowCount).Cells(2).Style.Format = Format("dd-MM-yyyy")
            xRowCount = xRowCount + 1
        Loop

again with no effect

I'm guessing it's not changing because it doesn't recognise the cell as a date, instead it's just a string.

I've attempted to convert it to a date but am also failing - and I think that's because the format it receives is seen as incorrect.

I know it's an ugly way to do things, but using some string manipulation I got it show how I want.

 Dim x As Integer
        Dim date_read As String
        Dim strArr() As String
        Dim part1 As String
        Dim part2 As String
        Dim part3 As String
        Dim new_date As String

        For x = 0 To DataGridView1.Rows.Count - 1
            date_read = DataGridView1.Rows(x).Cells(2).Value
            strArr = date_read.Split("/")
            part1 = (strArr(0))
            part2 = (strArr(1))
            part3 = (strArr(2))
            new_date = (part2 & "/" & part1 & "/" & part3)
            DataGridView1.Rows(x).Cells(2).Value = new_date
        Next
        DataGridView1.Refresh()

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40538224
To your statement, "I'm guessing it's not changing because it doesn't recognise the cell as a date, instead it's just a string.", then you would be guessing  correctly. Because it is in string format you will need to go about this in another way. Is it possible to create a new collection of the data in the DataGridView so that the date field is of type DateTime? Or change the source data type to the field to be DateTime?
0
 
LVL 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 40539595
it is a string being returned so you have to rearrange things
Dim TestArray() As String = Split(TestString,"/")
// now recombine
Teststring = TestArray(1) + "/" + TestArray(0) + "/" + TestArray(2)
0
 
LVL 1

Author Comment

by:amaru96
ID: 40539691
Thanks, but that's pretty much what I did - although not as elegant ;-)

However, that does seem to be the answer, so thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now