0

I am passing a date time:

    2015-12-23T09:57:00.000Z

To a stored procedure in mariaDB. The stored procedure doesn't like the javascript date/time, how do I convert the date/time for use in the stored procedure?

I've been playing around with 'str_to_date':

    select str_to_date('2015-12-23T09:57:00.000Z', '%Y-%M-%dT%h:%i:%s');

However this returns (NULL).

1
  • 1
    Try sending it as string using Date.toLocaleString() Commented Dec 28, 2015 at 10:10

3 Answers 3

2

1) Alternative pass string into stored procedure and then in sql convert to datetime like this:

select CONVERT(DATETIME,REPLACE(REPLACE('2015-12-23T09:57:00.000Z','T',' '),'Z',''))
Sign up to request clarification or add additional context in comments.

1 Comment

This works, as long as you add the conversion type: SELECT CONVERT(REPLACE(REPLACE('2015-12-23T09:57:00.000Z','T', ' '), 'Z', ''), DATETIME);
1

You can use timestamp, in JS get UNIX timestamp like so

var now = new Date(),
    unixTimestamp = Math.floor(now.getTime() / 1000);
    // JS operates with miliseconds, divide by 1000 to get seconds

And in mariaDB use function FROM_UNIXTIME(timestamp)

Comments

0

Your %M should have been %m.

Another approach:

+-----------------------------------------------------------------------+
| str_to_date(LEFT('2015-12-23T09:57:00.000Z',19), '%Y-%m-%dT%h:%i:%s') |
+-----------------------------------------------------------------------+
| 2015-12-23 09:57:00                                                   |
+-----------------------------------------------------------------------+

If you have 5.6.4 or later:

+-----------------------------------------------------------------+
| str_to_date('2015-12-23T09:57:00.987Z', '%Y-%m-%dT%h:%i:%s.%f') |
+-----------------------------------------------------------------+
| 2015-12-23 09:57:00.987000                                      |
+-----------------------------------------------------------------+

Comments

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.