Sign in to follow this  
Followers 0

3 posts in this topic

Posted

Hi,

I'm trying to convert a string into a date type. This is the wasy bit but I want to convert it into a US format from a GB format. The code I have is as follows


string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

DateTime myDate = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

myDate defaults back to the GB style date.

Thanks,

Share this post


Link to post
Share on other sites

Posted

Solved my problem, the reason I was trying to convert the Date was to update a sqlserver database with the date format it required. If anyone else comes across this problem you need to user sql parameters e.g.


string sqlstmt = "UPDATE [tablename] Set [dateColumn]=@Date"

SqlCommand sqlCmd = newSqlCommad(sqlstmt, [your sql connection]);

sqlCmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime));

sqlCmd.Parameters["@Date"].Value = DateTime.Now;

sqlCmd.ExecuteNonQuery();

'where sqlCmd is the SqlCommand

Share this post


Link to post
Share on other sites

Posted

A DateTime doesn't have a set style, internally it's just a bunch of integers and longs that are completely independent from culture. It's only when you call ToString() that it produces a string representation, and you can control what culture that representation is in by passing the correct CultureInfo parameter - exactly like when you use Parse or ParseExact.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.