Steev

I would really appreciate an expert to help walk me through date formatting, I*ve been trying for several weeks but I*m going nowhere! A few weeks ago I posted on this forum about ※is the date format dependant on a computers* locale § (posted 16 January 2007). I*ve tried very hard to understand and work out the helpful answers given but I*m still confused.

I*d like to learn how to format a date in different ways 每 so that the date will always be processed in the format I*ve given a programme.

This is code by DMan:

Dim dti As New Globalization.DateTimeInfo()

&This sets the short date pattern only#you should be thorough and set all patterns when using custom formats.

Dti.ShortDatePattern = ※yy/dd/MM§

&This sets your custom date time format to the current culture of the PC

Globalization.CultureInfo.CurrentCulture.DateTimeFormat = dti

And this is code by Renee:

Thread.CurrentThread.CurrentCulture = new CultureInfo("Your

Culture designation here")

1. The way I interpret this code 每 globalization means all the programme I*ve written can ※see§ the new format - is this correct

2. I don*t understand culture, what is this Does this mean the current computer settings Or does it mean new settings I have made and applying to a programme

2. With this code, I*m not sure where to put it 每 do I declare it publicly or do I declare it in a private sub

3. What happens after I*ve put it in the programme ie will entered data be automatically set in this new custom date format Or do I need to do anything else to it

Basically, I just don*t know what to do and have been getting nowhere, and I*d appreciate your help.

What I ultimately plan on doing is to write a database-type programme and save the entered data but for now I would just like to learn about custom date formats.

Sorry for the long complicated post!! Basically, I*d like to learn about custom formats and how to do them, and where to put any code. The format I*d like to do for this project is yy/dd/MM

Thanks for your help and advice.

Steve


Re: Visual Basic Express Edition Date Format - Globalization

Steev

Anyone.......




Re: Visual Basic Express Edition Date Format - Globalization

Andreas Johansson

First of all you have to distinguish between how you save it and how you display to user and read their input.

There is nothing that will explain how you save a date in a file, database, xml or whatever. That is entirely up to you and how you want to do it. Formatting, localization, globalization is mostly about presentation when it comes to dates.

A good way to save date/times is converted to UTC. First step to do this is to setup the local computer with a source of time and what timezone it is in. When you want to save a datetime you will use the ToUniversalTime() method to get the UTC time and store it. All clients around the globe will only store that time to be able to tell the sequence of saves.

When you read date/times stored as UTC date/times you can use constructors of DateTime to convert it to the local time.

These methods will help you persist time in any timezone in a way that can be read back in any timezone and it will be the same time in local time.

How you display a date should not be up to you but the user and the settings he/she has choosen. I have choosen to display dates as dd/mm/yyyy and by default any DateTime will be formatted as that. If you need to format dates by yourself you should use the CultureInfo from Thread.CurrentUICulture (or maybe CurrentCulture, I am never sure). However, the actual format should never be up to you as a developer to choose but me as a user to choose.






Re: Visual Basic Express Edition Date Format - Globalization

Steev

Hi Andreas,

Thanks for your reply.

I work on ships and learning VB is a very satisfying "off shift" hobby. As a source, I'm using all the nautical charts on board to learn about writing a database programme (adding and deleting records).

When the Hydrographic office release a new chart they always print the print date on the back of the chart in the format "yy/dd/MM" and this is the format that's officially used for charts on any ship! So while I agree that it is "user choice" for style, in this case it's the "yy/dd/MM" format that is the official format and I just want to keep with it.

Any help will be greatly appreciated###..!!!!

Regards,

Steve




Re: Visual Basic Express Edition Date Format - Globalization

Andreas Johansson

I haven't seen your other posts so I am not sure I get what you need to with the date in the "yy/dd/MM" format.

Would you want your application to accept input in that format Then internally in your application you work with it as a DateTime structure and when displaying it back to the user you would like to display it in that format

The DateTimePicker control will allow you to set what format the date is displayed as but it will return the inputed value as a DateTime value, so no need to worry about if it is a correct date. The control do all the work.

Add a DateTimePicker control to your form and change the Format property to be Custom and the CustomFormat to be "yy/dd/MM".

If just need to display the date us the DateTime.ToString("yy/dd/MM") function to get a string with the date formatted like that.

Internally always work with the DateTime structure, then you will never have to worry about what format it might be in. The controls should do that for you.






Re: Visual Basic Express Edition Date Format - Globalization

Steev

Hi Andreas,

Yes, I'd preferably like the data to be input in that format because that's what the "source" data is in.

In my mind, the idea I have is to keep things simple so I thought - use a textbox and the user types in 6 numbers eg 982010, this is for 1998, 20th October - and then it is saved in the database as 98/01/20. As I said earlier, this format is printed on all ship charts and is the format we're all used to seeing!! And yes, also display it in that format.

I will be displaying the date in this format when the user looks at the records on the screen and also after printing a report, and I'll also be doing a simple calculation with it (is it a newer or older date than "abc" which will be in the same format).

I haven't tried using a DateTimePickerControl - I'm still in the early stages of learning VB - I didn't know it could do formats in this way. In my first post on this subject (back in January) I got an answer from DMan and ReneeC and they suggested I use the custom culture method and because I don't know any better, that's what I attempting!!

Thanks, Andreas, I appreciate your help!!

Regards,

Steve






Re: Visual Basic Express Edition Date Format - Globalization

Andreas Johansson

Usually there is a few ways to get things solved. Try playing around with the DateTimePicker control, it will easily allow the user to enter dates in the format you want, and internally you get it as a DateTime. No need to worry about the format internally or having to change the culture.






Re: Visual Basic Express Edition Date Format - Globalization

Steev

Just to avoid any confusion 每 I don*t want to reset the entire computer to a new date format 每 I*d just like to learn about date formatting for the project I*m working on.

I*d really appreciate someone*s help on this. The problem is in the yellow highlighted part, I get the error message ※Instance is read only§ and I think it's something to do with the current culture part.

Andreas suggested the DateTimePicker but unfortunately it isn*t the correct tool for this project. I have a textbox and I want to enter 6 numbers eg 982304 - the ShortDatePattern will change it to 98/23/04 (for 23rd April 1998). I will then save this in a database, which I can do, it*s this custom formatting that has me beaten.

Thank you for your advice and help. Previous post on same topic = 16th January 2007.

Please could someone help me###. Here is my code:

Regards,

Steve

Imports System.Globalization

Public Class Form3

Dim ADate As Date

Dim dti As New Globalization.DateTimeFormatInfo

Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'This sets the short date pattern only...you should be thorough and

'set all patterns when using custom formats

dti.ShortDatePattern = "yy/dd/MM"

'This sets your custom date time format to the current culture of the PC()

Globalization.CultureInfo.CurrentCulture.DateTimeFormat = dti

The Problem is here 每 every time I open this form I get the error message: ※Instance is read only§

Globalization.CultureInfo.CurrentUICulture.DateTimeFormat = dti

End Sub

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

ADate = Date.Parse(TextBox1.Text)

TextBox2.Text = CStr(ADate) &this textbox is only to show the new date while I*m practising on this project.

End Sub

End Class






Re: Visual Basic Express Edition Date Format - Globalization

Andreas Johansson

Did you try the DateTimePicker and entereing dates into it You do not have to set it by using the mouse but can also enter the date as numbers when it has focus. Almost just like you would in a textbox.

It will work almost like a textbox and avoid all this problems you are having with changing date formats in the culture of the application.