Solved

VB.NET change date from US format

Posted on 2015-01-07
6
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 81

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 81

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

738 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