Solved

DateTime in US format

Posted on 2006-06-16
5
408 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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