DateTime dt = DateTime.Now;
System.Globalization.CultureInfo uk = newSystem.Globalization.CultureInfo("en-GB");//creates a new CultureInfo object based upon a culture code, here en-GB
string s =dt.ToString(uk.DateTimeFormat);
string s2 = dt.ToString("dd.MM.yyyy hh:mm:ss");//manualy prints a date, it can be set to fit what ever you need
//d: day
//M: month
//y: year
//h: hour
//m: minute
//s: second
I can display is by converting it to .ToString("dd/MM/yyyy") but how to deal with user input on a form which is a string so that the server in US knows what to store.
That is what I do with Access database, define date format in design view, but SQL compact database has not got that option.
If I use HTML 5 tag of 'date' as ( <input type="date" ) I see the date in local format and correct date is inputed as well in the database.
However in the 'Edit' page when I try to show that date using value="@duedate" it will show the default text of Day/Month/Year of date tag of HTML5 . So I have to use input type="text" to show the date from the database - which then appears in US format
unless I use
var duedate = String.Format("{0:dd/MM/yyyy}", transaction.DueDate);
I will get the correct date format but if I use same format to change the date that will get converted into US format at the server in US and incorrect date will get saved in the database or will give error if it is invalid.
That is what I want to ask how to manipulate this at the level of form submit with Request.Form["duedate"] level to get the correct format.
Hope it helps in answering my query which I am unable to solve.
If you are storing dates as dates as opposed to strings, don't worry about your database's format. Just make sure that you update it with a date object.
If your datepicker is displaying the date in UK format, hopefully all your users are British. Otherwise you will be confusing people. However, once your form is submitted, you have a string in a predictable format. Cast it to a date and insert it into
your db.
SHR
Member
30 Points
63 Posts
DateTime Format UK/US
Nov 30, 2012 07:34 AM|LINK
Dear All
I am struggling with this problem, and after spending countless hours reading still could not solve it.
The web server stores date in US date format and my datepicker displays date in UK date format.
so when submitting the form either the wrong date is stored or it generates error if it is not the valid date
e.g. 02/07/2012 (July 2nd) in UK date format on the form get changed to 07/02/2012 (Feb 7th)
while 29/11/2012 in UK date format gives error that it is not valid date.
Please help
Hamid
RameshRajend...
Star
7983 Points
2099 Posts
Re: DateTime Format UK/US
Nov 30, 2012 07:39 AM|LINK
DateTime dt = DateTime.Now; System.Globalization.CultureInfo uk = newSystem.Globalization.CultureInfo("en-GB");//creates a new CultureInfo object based upon a culture code, here en-GB string s =dt.ToString(uk.DateTimeFormat); string s2 = dt.ToString("dd.MM.yyyy hh:mm:ss");//manualy prints a date, it can be set to fit what ever you need //d: day //M: month //y: year //h: hour //m: minute //s: secondSHR
Member
30 Points
63 Posts
Re: DateTime Format UK/US
Nov 30, 2012 08:46 AM|LINK
Thank you Ramesh for prompt reply
I can display is by converting it to .ToString("dd/MM/yyyy") but how to deal with user input on a form which is a string so that the server in US knows what to store.
var dt = Request.Form("duedate");
thanks
Hamid
raja01
Member
76 Points
72 Posts
Re: DateTime Format UK/US
Nov 30, 2012 10:47 AM|LINK
hi SHR
If u store the Date in D.B Format (MM/DD/YYYY --- 4/15/2010 12:00:00 AM)
So u get the error.
Please change the format and store the data ...
S.Raja Durai
SHR
Member
30 Points
63 Posts
Re: DateTime Format UK/US
Nov 30, 2012 08:29 PM|LINK
Thank you Raja for your input
That is what I do with Access database, define date format in design view, but SQL compact database has not got that option.
If I use HTML 5 tag of 'date' as ( <input type="date" ) I see the date in local format and correct date is inputed as well in the database.
However in the 'Edit' page when I try to show that date using value="@duedate" it will show the default text of Day/Month/Year of date tag of HTML5 . So I have to use input type="text" to show the date from the database - which then appears in US format unless I use
var duedate = String.Format("{0:dd/MM/yyyy}", transaction.DueDate);I will get the correct date format but if I use same format to change the date that will get converted into US format at the server in US and incorrect date will get saved in the database or will give error if it is invalid.That is what I want to ask how to manipulate this at the level of form submit with Request.Form["duedate"] level to get the correct format.Hope it helps in answering my query which I am unable to solve.ThanksHamidoned_gk
All-Star
31017 Points
6352 Posts
Re: DateTime Format UK/US
Dec 01, 2012 08:09 AM|LINK
try
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.71).aspx
SHR
Member
30 Points
63 Posts
Re: DateTime Format UK/US
Dec 01, 2012 09:52 PM|LINK
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: DateTime Format UK/US
Dec 01, 2012 10:10 PM|LINK
If you are storing dates as dates as opposed to strings, don't worry about your database's format. Just make sure that you update it with a date object.
If your datepicker is displaying the date in UK format, hopefully all your users are British. Otherwise you will be confusing people. However, once your form is submitted, you have a string in a predictable format. Cast it to a date and insert it into your db.
SHR
Member
30 Points
63 Posts
Re: DateTime Format UK/US
Dec 03, 2012 09:28 PM|LINK
I am now using HTML5 date type which is working fine in Chrome and Opera but not in IE
Thanks for all the help
Hamid