0

In my C# project I need system date time to another specific date time format.

My system datetime format is like "15/03/2017 9:25 AM" --->that can be changed according to computer wich my program run.

But I need to parse that datetime format to another date time format something like "2017-03-15 9:25 AM"----> save date in SQL datebase accept this format only.

I need Assign this to variable to Datetime Variable not need save in String variable.

I tried this code but not working

string datetimesss = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")

It returns

// value of datetimesss="2017-03-15 09:33:25"

Then i parse again to date time format

 DateTime dates = DateTime.Parse(datetimesss);

It returns

// value of dates ="15/03/2017 9:42:43 AM"

Convert back to my computer datetime format .How can I convert any datetime format to DateTime "yyyy-MM-dd hh:mm:ss"

Thank You!

J.SMTBCJ15
  • 471
  • 6
  • 20
  • This might help on your question. [DateTime Formatting](https://msdn.microsoft.com/en-us/library/5hh873ya(v=vs.100).aspx) – P. Pat Mar 15 '17 at 04:24
  • What is your actual requirement? what is the type of input that you are dealing with? – sujith karivelil Mar 15 '17 at 04:24
  • what is the **expectedInput** and **expectedOutput**, this is a bit unclear – Jaya Mar 15 '17 at 04:26
  • 4
    [Sql server does not store date display format.](http://stackoverflow.com/questions/30032915/how-to-cast-the-datetime-to-time/30033028#30033028). Nor does c#. Further more, c# DateTime struct maps directly to Sql Server DateTime data type, so there really is no need to bother with string representation formats. Just pass dates as parameters and you'll be fine. – Zohar Peled Mar 15 '17 at 05:27
  • @un-lucky 'DateTime.Now' gave current system time.but that can be change according to computer user requirement. how i Assign current system date time to DateTime variable format Like ("yyyy-MM-dd hh:mm:ss") ? – Gayan Chinthaka Dharmarathna Mar 15 '17 at 06:37

3 Answers3

2

You should avoid strings and just keep your data in DateTime variables. ADO.Net already knows how to translate between .NETs DateTime and SQL Server's datetime - and neither of these has a format (both of them, internally, are just numbers. .NETs DateTime is a count of 100ns intervals since 01/01/0001. SQL Server's datetime is a count of whole and fractional days since 01/01/1900).

Something like:

var updateCommand = new SqlCommand(
         "UPDATE  [User] SET  Last_loign_date =@LastLoginDate"
         ,conn); //Assume conn is an SqlConnection object
updateCommand.Parameters.Add("@LastLoginDate",SqlDbType.DateTime).Value = DateTime.Now

or, in the alternative, why not use the database server time rather than passing it:

string sqlupdatepassword = "UPDATE  [User] SET  Last_loign_date =current_timestamp";
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
1

If sql server language is set as us-english then your date - > '15/03/2017 9:25 AM' will be considered as text and converting into date will not give correct results. You need create date based on day,month and year from your given date by using string functions. Use the below code to get required output :

declare @date varchar(30) = '15/03/2017 9:25 AM'

select  cast(left(parsename(replace(@date,'/','.'),1),4) + 
        parsename(replace(@date,'/','.'),2) + 
        parsename(replace(@date,'/','.'),3) as datetime) +
        ltrim(stuff(parsename(replace(@date,'/','.'),1),1,4,''))
sam
  • 1,242
  • 3
  • 12
  • 31
0

Finaly I got the point, I parse DateTime to String

string sqlupdatepassword = "UPDATE  [User] SET  Last_loign_date ='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss tt") + "'";

In SQl database "Last_loign_date" is datetime format. this may not correct, but this worked for me.

thanks!

  • You got the *wrong* point. It's only when you're treating the dates as strings that they even *have* a format. If you just keep your data in `DateTime` variables, and *avoid* strings, there's no conversions to perform. When you interact with SQL Server, you can keep your data as `DateTime` data by using *parameters* - which is also a good habit to get into using anyway since it's the safest way to prevent SQL Injection. – Damien_The_Unbeliever Mar 15 '17 at 08:24
  • @Damien_The_Unbeliever If I upadate like this its not working.return exception "ERROR [22007] [Microsoft][ODBC SQL Server Driver][SQL Server]The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. ERROR [01000] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated." – Gayan Chinthaka Dharmarathna Mar 15 '17 at 08:33
  • But that's because, somewhere along the line, you've again converted your `DateTime` value into a *string*. If you can *avoid* the string conversions, you can *avoid* formatting issues. – Damien_The_Unbeliever Mar 15 '17 at 08:39