Solved

VB.NET change date from US format

Posted on 2015-01-07
6
249 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

11 Experts available now in Live!

Get 1:1 Help Now