Solved

DateTime in US format

Posted on 2006-06-16
5
401 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now