Solved

DateTime in US format

Posted on 2006-06-16
5
409 Views
Last Modified: 2009-12-16
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.
0
Comment
Question by:jasoncpp
  • 4
5 Comments
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16925310
using System.Globalization;
-------------------------------------------
System.DateTime a=System.DateTime.Now;
DateTimePickerFormat  nfi = new CultureInfo( "en-UK", true ).DateTimeFormat;
0
 
LVL 7

Accepted Solution

by:
pradeepsudharsan earned 500 total points
ID: 16925343
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-US" uiCulture="en-US"/>

add this line in web.config
or
      System.DateTime a=System.DateTime.Now;
      DateTimeFormatInfo nfi = new CultureInfo( "en-US", true ).DateTimeFormat;
      MessageBox.Show(a.ToString(nfi));
            
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16926021
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

0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16926022
sorry the final one is wrong post
0
 

Author Comment

by:jasoncpp
ID: 16926357
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.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question