3

I'm getting date from java script and want to store in sql table by executing c# code.

How to do?

java script format: Mon Oct 15 15:34:18 UTC+0530 2012

mssql format: 2012-10-15 16:18:04.000

3
  • how about storing it as a timestamp (long)? And then converting and rendering as needed by the UI. Commented Oct 15, 2012 at 12:02
  • do you think you are the first person on this planet solving this problem? :) have you tried searching? see Javascript date to C# via Ajax or Parsing Date-and-Times from JavaScript to C# Commented Oct 15, 2012 at 12:03
  • yes. i tried but not able to do Commented Oct 15, 2012 at 12:05

1 Answer 1

5

You may parse the JavaScript date to C# DateTime type (using DateTime.ParseExact) and then save it in SQL server using Parameterized query. For your date format you can try:

string str = "Mon Oct 15 15:34:18 UTC+0530 2012"; //your javascript date as string
DateTime dt = DateTime.ParseExact(str, 
                                  "ddd MMM d HH:mm:ss UTCzzzzz yyyy", 
                                   CultureInfo.InvariantCulture);
Sign up to request clarification or add additional context in comments.

7 Comments

this is my code Dim time As String = DateTime.ParseExact(hdnTime.Value, "ddd MMM d HH:mm:ss UTCzzzzz yyyy", InvariantCulture) but not able to store..its throwing error
@balaji, my answer is in C# , I have just divided the DateTime.Parse call to multiple lines for readability
my run time query INSERT INTO payu_transactions(txn_id,txn_customerid,txn_amount,txn_datetime)VALUES('345785','30','656','15/10/2012 17:41:59')
getting this error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
@balaji don't concatenate the values; just pass the value down as a parameter. If you ever need to know what the date-format is on a database server, then you're doing it wrong™. Your SQL should look like .... values(@id, @name, @date, ...)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.