Hemant Hindlekar

Hello

Can somebody help me for below problem

In my .Net application I am using vb component having one method which accepts DateTime as a parameter. This value will be inserted into the database.
The value to be passed to this method will be taken from some other database.
Before passing this datetime variable I want to set the date format as a current culture system datetime format with ShortDatePattern.
I used the below code to get the system short date format and to set that format to DateTime variable but the format is not getting changed to ShortDatePattern.

Code :-
string LstrSystemDateFormat = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
string LstrDateString = "15/01/2007";
DateTime EffectiveDate = DateTime.ParseExact(LstrDateString, LstrSystemDateFormat,System.Globalization.CultureInfo.CurrentCulture);

If I check the value for EffectiveDate it shows me "01/15/2007". why is it behaves like this. Any reason

Regards
Hemant




Re: Visual C# General Converting date to system current date time format

Ross Dargan

silly question but what exactly did you expect to come back This could infact be correct depending on your global settings!

Also on a side not I would recomment using DateTime.tryParse() incase the string is not valid - take a peek at my blog http://blog.the-dargans.co.uk/2007/01/try-get-design-pattern.html) for more infor.

Ross Dargan






Re: Visual C# General Converting date to system current date time format

Mark Dawson

Hi Hermant,

when you are parsing in the string you need to specify the format that the DateTime string is in i.e. MMDDYYYY to let the DateTime object know how to interpret the data, inside the DateTime object this date is stored without culture formatting. Once you have parsed the data in, then when you print it out again you need to specify also how you want that information to be formatted.

For example in the code below the date in the string is in the format DD/MM/YYYY, when you parse it you need to tell the DateTime that format, in the case below I tell it that the date is in en-GB format which is DDMMYYYY, however if I had specified en-US then it would have failed because it would have expected MMDDYYYY. If you do not specify then it will take the culture info from the current thread.

Then once the data is read in the code prints it back out in different formats based on the culture of the thread. What you need to do is to store your data without being language specific in your database that is important or you will have issues. I suggest that you store data using the System.Globalization.CultureInfo.InvariantCulture formatting, you can then save and parse data using this format, only when you actually want to display the datetime do you specify someother culture like Fr-FR.

Hope that makes sense.

Mark.

using System;

using System.Globalization;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

string date = "15/01/2007";

DateTime dt = DateTime.Parse(date, new CultureInfo("en-GB"));

//This will print 01/15/07

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

Console.WriteLine("en-US: " + dt.ToString(System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern));

//This will print 15/01/07

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

Console.WriteLine("fr-FR: " + dt.ToString(System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern));

Console.WriteLine("Invarant: " + dt.ToString(System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern));

}

}

}






Re: Visual C# General Converting date to system current date time format

boban.s

I think that this code is not as you expose. There must be something between, which probably change CurrentThread culture. Because when you want to see what is in EffectiveDate, what is presented is the ToString() result of the variable. So if You did succesfully parse the data as you write "15/01/2007" that means ShortDatePattern is dd/MM/yyyy. But when you check the variable it is presented as in format MM/dd/yyyy. Probably you have changed the CurrentThread to "en-US" but you didn't return it back to the previous one. To make a check why don't you check ShortPatern at the point when you check EffectiveDate value.

Also, using ParseExact for converting string to date is the right way to do. TryParse and even more Parse should not be used for DateTime.






Re: Visual C# General Converting date to system current date time format

Ross Dargan

boban.s wrote:

Also, using ParseExact for converting string to date is the right way to do. TryParse and even more Parse should not be used for DateTime.

oh, could you expand

Thanks

Ross






Re: Visual C# General Converting date to system current date time format

boban.s

When you try to convert a string representing the date and that conversion to work regardless of the current user settings you should use ParseExact. TryParse will return invalid conversion and what with that. When converting date string you know the format of the string but you don't the current date format. For example :
string dateString = "01/15/2007";
if the current format of date is "dd/MM/yyyy" or even "M/d/yyyy" the conversion will fail. Using TryParse you can see that will fail but what to do with that result.




Re: Visual C# General Converting date to system current date time format

Ross Dargan

Does this not mean that if you use

string dateString = "09/04/07"

you wont know which result you are going to get

Anyway a bit of topic, thanks for the reply though.

Ross






Re: Visual C# General Converting date to system current date time format

boban.s

If you don't know what is the format of the string, then using any method will be a problem. ParseExact then is unusefull but TryParse or any other are unusefull too. TryParse can be used only when you let user to enter date in free format and because you don't know the format you need to use TryParse. But imagine if this's a posible scenario. You will probably not use text box for datetime field. When you develop everything by your own you try to work in typed manner, using calendar controls or any third party datetime controls which by them self validate user entry and return datetime type of value. What you can't control is colaboration with other systems, where if you work with text file format you will have always a date in exact string format.




Re: Visual C# General Converting date to system current date time format

Ross Dargan

You are quite correct - tryparse does have it uses, here is not one of them!

Ross






Re: Visual C# General Converting date to system current date time format

Hemant Hindlekar

Hi Mark

For below code if I check the value for dt in QuickWatch window it still shows the date as "01/15/2007".

string date = "15/01/2007";

DateTime dt = DateTime.Parse(date, new CultureInfo("en-GB"));

When the date is stored in database it is stored in correct format.

Does it means that in Quickwatch the datetime object always shows the value in 'mm/dd/yyyy'

Regards

Hemant






Re: Visual C# General Converting date to system current date time format

Hemant Hindlekar

Hi

I have not changed the current thread to "en -US". The ShowDatePattern is showing in DD/MM/YYYY.

Regards

Hemant






Re: Visual C# General Converting date to system current date time format

Mark Dawson

Hi,

you specify the en-GB in the parse to verify the format of the string to parse in, howver when you look at the value in Visual Studio and it shows mm/dd/yyyy then your copy of VS or your OS must have that date formatting as the default date formatting. If you use ToString like in my previous example where you specify the CultureInfo to use as formatting then you will see the correct value.

Mark.






Re: Visual C# General Converting date to system current date time format

Mark Dawson

Hi,

DD/MM/YYYY would be correct for a german setting i.e. de-DE, if you have changed the date format to en-US then it would have been MM/DD/YYYY.

Mark.






Re: Visual C# General Converting date to system current date time format

Hemant Hindlekar

Hi

The OS format is dd/mm/yyyy. I have checked it with below code.

string LstrSystemDateFormat = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern;

Where can i find the date format in VS

Hemant