0

I coded a program for work to keep tracks of our projects linked to an access database. The code is written in VB.NET

The thing is I use a computer with dates in French. The whole thing is coded according to that language. But now I have to install the program on all the computers in the company (some are in French and som in English). I can't change the language of the english computers because of another program they're using.

So how can I make my program to work with English dates?

I tried to detect the language of the computer this way:

CultureInfo.CurrentCulture.DisplayName

And then to convert the Today date to French (I'm using the Today date to compare it to a due date for "Alarms" to prevent us when a project is late or due for today):

Today = Today.toString(CultureInfo.CreateSpecificCulture("fr-CA")

But this doesn't seem to be the right way to do it since my program doesn't load afterwards.

If you have any idea, I'm willing to read them

Thanks guys

  • Most likely you are actually creating a problem where there isn't one. The only reason that you would need to care about date format is if you need to read dates from storage as text or save to storage as text. Under any other circumstances, dates are binary, i.e. just numbers, and have no format. For instance, if the user enters a date via a `DateTimePicker` control then every user can do so using the date format appropriate to their own system while, in code, you get the `DateTime` value from the control's `Value` property and that is independent of any format. – jmcilhinney Apr 24 '17 at 14:32
  • Please explain EXACTLY how you are using dates in your app so that we can determine whether there is even a problem to solve. I expect that there isn't. – jmcilhinney Apr 24 '17 at 14:33
  • When someone creates a project, he has to choose a delivery date with a DateTimePicker then the project is saved to the Access Database. Then you have a DataGridView that shows the projects where you have a column "Alarm". If the delivery date is higher than the today's date then you have an alarm (the cell turns red). Right now, if the computer is set to English, nothing appears in the alarm column – Simon Stifler Apr 24 '17 at 14:58

1 Answers1

0

Based on that description, there is no problem other than the one you are creating. DO NOT convert dates/times to Strings unless you actually need Strings. You do not.

In the case of the DateTimePicker, you simply set the Format to Long or Short and the user will then see dates in a format appropriate to their system, based on their current culture settings. In code, you get a DateTime value from the Value property and that is a binary value, so format is irrelevant.

In the case of the DataGridView, if you have a column that contains DateTime values then they will be displayed in a format based on the current culture. The underlying values are binary so they have no format but the grid must use a format for display purposes. Each user will see what they expect because the system's culture settings will be used to perform that format. If you don't like the format used, you can set the DefaultCellStyle.Format property of the column to "D" or "d" to match the Long and Short formats of the DateTimePicker respectively.

As I said, the values in the cells of such a column are DateTime values, not Strings, so format is irrelevant. If you want to compare them with today's date then you do so in binary format, e.g.

If CDate(myDataGridViewCell.Value) > Date.Today Then

At no point do you have to worry about format because the application will use the current system culture settings automatically anywhere that format is an issue.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46