Click here to Skip to main content
15,895,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi all

In my application what i am doing now is i am saving the user choice datetime format in database table..
e.g. user selected the format as mm-yyyy-dd

so now while displaying the datetime to the UI i have to convert the datetime format to the user defined format.
I have a datetimepicker for selecting the date

Please tell me how to do this..
Posted
Comments
Maciej Los 5-Aug-13 8:38am    
What have you tried so far?
Where are you stuck?
[no name] 5-Aug-13 8:39am    
that's what i am not getting how to start..i have saved the user format to database..
but how to convert to that format..?
Maciej Los 5-Aug-13 15:55pm    
Are we talking about formatting date on MS SQL Server side or on client side using VB.NET, C# or any other programming language?
Bernhard Hiller 5-Aug-13 9:34am    
Windows Forms or WPF? In the latter you are (almost) lost: WPF does not support custom formats in its DateTimePicker.

I think I answered this elsewhere recently.

Data formatted per user preferences should never end up as stored data. The only place that user preference should be effective is when displaying the data to the user through the UI, and likewise going the other way when translating a user specified time/date to internal structure. The date data should be used in memory and persisted to storage in a consistent form that you, the programmer, choose.

My personal recommendation for all stored time/date fields is ISO 8601[^].
 
Share this answer
 
Comments
Maciej Los 5-Aug-13 16:17pm    
Good advice!
+5
OK, there are 2 issues to discuss with concerning with date formatting:
1) formatting date on MS SQL Server side,
2) formatting date on client side using VB.NET, C# or any other .NET programming language


ad 1)
To format date on server side, see SET DATEFORMAT statement[^]. Note: there are some restrictions.
I would recommend you to use ymd format - see H.Brydon solution/recommendation ;)


ad 2)
Formatting date in VB.NET or C# is simple. See this: Custom Date and Time Format Strings[^].

Summary:
If you can get the date and string-date-format from database, you can do everything what you want to do ;)

To change string-date-format for DateTimePicker, see this: Displaying a Date in a Custom Format with the Windows Forms DateTimePicker Control[^]

Is it enough for start?
 
Share this answer
 
Comments
Raja Sekhar S 6-Aug-13 1:25am    
Nice.. +5!
Maciej Los 6-Aug-13 1:33am    
Thank you, Raja ;)
Raja Sekhar S 6-Aug-13 2:20am    
You are Welcome...
check this hope it help :

datetime-format[^]
 
Share this answer
 
Comments
Maciej Los 5-Aug-13 16:18pm    
This question is tagged as VB.NET.
A4!
Use the DateTimePicker.CustomFormat Property[^]. To avoid conversions of the format string, save the format string in the form that is used by the DateTimePicker.
 
Share this answer
 
Comments
Maciej Los 5-Aug-13 16:20pm    
Good advice, but not completely, sorry ;(
Assumning your problem is only with the date formatting Try this
C#
string usrFormat = "";
// put code to retrieve the format from the database and assign  it to usrFormat variable 
//Assuming you have a datetime picker named dtp 
dtp.Format = DateTimePickerFormat.Custom;
dtp.CustomFormat = usrFormat; 


Hope it helps
 
Share this answer
 
Comments
Maciej Los 5-Aug-13 16:21pm    
A4!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900