Solved

VB.NET change date from US format

Posted on 2015-01-07
6
260 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 80

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 63

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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 63

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 80

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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