Link to home
Start Free TrialLog in
Avatar of jasoncpp
jasoncpp

asked on

DateTime in US format

Hi,

How do you convert a datetime format from one format to any other.  In particular, US to UK format?  I am using sql convert(datetime, '12/05/2006',103) when using dates in sql, but when a date is in US format it causes a problem.  I am using a calendar control, perhaps there is another way around this, such as forcing the control to use a specific locale?

Thanks.
Avatar of pradeepsudharsan
pradeepsudharsan

using System.Globalization;
-------------------------------------------
System.DateTime a=System.DateTime.Now;
DateTimePickerFormat  nfi = new CultureInfo( "en-UK", true ).DateTimeFormat;
ASKER CERTIFIED SOLUTION
Avatar of pradeepsudharsan
pradeepsudharsan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
First convert the string to DateTime format
Construct new Sql DateTime parameter and assign the converted datetime value to it.
Pass it to stored procedure.

string intSaleDate =txtSaleDate.Text;      // this is where the problem starts
                              
DateTime a;
a=DateTime.Parse(intSaleDate);
SqlCommand cmdObj = new SqlCommand("MLS_GetData", connObj);
cmdObj.CommandType = CommandType.StoredProcedure;


if (txtSaleDate.Text != "") ///////////// here is the FROM date //////////////
{
    SqlParameter var1=  new SqlParameter("@intSaleDate",SqlDbType.DateTime);
    var1.Value=a ;
      cmdObj.Parameters.Add(var1);
}


Regards
Pradeep

sorry the final one is wrong post
Avatar of jasoncpp

ASKER

Couple of extra questions, if you don't mind - thanks for good answer.

1. does the below line web.config guarantee that all posted data will be converted to the specified culture.  Can I ensure, for example, all data posted through a calendar picker will be in a certain date format? It appears to do this, as DateTime.Now returns the date format specified in web.config

<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>

2. Is there a way of returning a DateTimeFormatInfo object for the current machine/server, assuming the above line is not in web.config?

  System.DateTime a=System.DateTime.Now;
     DateTimeFormatInfo nfi = new CultureInfo( "en-US", true ).DateTimeFormat;
     MessageBox.Show(a.ToString(nfi));

Thanks.