Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript date to sql date object

Tags:

I'm trying to write a query that takes a Javascript date object and then puts it in an object type that is recognized by both SQL Server and Oracle database types.

The issue is that I'm using webservices. So it has to be a string, not an actual passed parameter. Here's what I mean:

var date = new Date(); var firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1); var lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);  var webServicesQueryWhereClause = 'readDate BETWEEN '+firstDayOfMonth+' AND '+lastDayOfMonth; 

Except firstDayOfMonth and lastDayOfMonth are surrounded by something like to_date() to actually put them in a date format that the databases can read. For example:

var webServicesQueryWhereClause = 'readDate BETWEEN to_date('+firstDayOfMonth+') AND to_date('+lastDayOfMonth+') '; 

What should I use to put those dates in a form that can be read by both SQL Server and Oracle?

like image 491
Graham Avatar asked Nov 19 '13 22:11

Graham


People also ask

How to get sql date in JavaScript?

var date = new Date(); var firstDayOfMonth = new Date(date. getFullYear(), date. getMonth(), 1); var lastDayOfMonth = new Date(date. getFullYear(), date.

How to get date from database in JavaScript?

JavaScript Date getDate() The getDate() method returns the day of the month (1 to 31) of a date.

What is the format for date in SQL?

SQL Server comes with the following data types for storing a date or a date/time value in the database: DATE - format YYYY-MM-DD. DATETIME - format: YYYY-MM-DD HH:MI:SS.

How do you find the time from a date object?

The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object. Parameters: The function does not accept any parameter. Return Value: It returns the number of milliseconds since January 1, 1970, 00:00:00 GTM.


2 Answers

Have you tried the solutions presented here:

Convert JS date time to MySQL datetime

The title should be called

"Convert JS date to SQL DateTime"

I happened to need to do the same thing as you just now and I ran across this after your question.

This is from the other post by Gajus Kuizinas for those who want the answers on this page:

var pad = function(num) { return ('00'+num).slice(-2) }; var date; date = new Date(); date = date.getUTCFullYear()         + '-' +         pad(date.getUTCMonth() + 1)  + '-' +         pad(date.getUTCDate())       + ' ' +         pad(date.getUTCHours())      + ':' +         pad(date.getUTCMinutes())    + ':' +         pad(date.getUTCSeconds()); 

or

new Date().toISOString().slice(0, 19).replace('T', ' '); 

The first one worked for me. I had a reference problem with the toISOString as well although I would prefer the one liner. Can anyone clarify how to use it and know the limitations on where one can reference it? Good luck!

like image 135
JPK Avatar answered Nov 02 '22 05:11

JPK


using MomentJs it will be pretty easy.

moment().format('YYYY-MM-DD HH:mm:ss') 

https://momentjs.com/

like image 25
jithil Avatar answered Nov 02 '22 07:11

jithil