Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.NET change date from US format

Posted on 2015-01-07
6
Medium Priority
?
274 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 84

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 64

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 64

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 84

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

885 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