From a7be73cc996d1e542a2fa64045225c8b5f8caf4e Mon Sep 17 00:00:00 2001 From: Mattijah Date: Sun, 1 Jun 2014 20:35:01 +0100 Subject: [PATCH] Added support for milliseconds --- lib/sql_string.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/sql_string.js b/lib/sql_string.js index 674ebf1b8a..b7eb304a6b 100644 --- a/lib/sql_string.js +++ b/lib/sql_string.js @@ -84,15 +84,16 @@ SqlString.dateToString = function(date, timeZone) { dt.setTime(dt.getTime() + (tz * 60000)); } } - + var year = dt.getFullYear(); - var month = zeroPad(dt.getMonth() + 1); - var day = zeroPad(dt.getDate()); - var hour = zeroPad(dt.getHours()); - var minute = zeroPad(dt.getMinutes()); - var second = zeroPad(dt.getSeconds()); - - return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; + var month = zeroPad(dt.getMonth() + 1, 2); + var day = zeroPad(dt.getDate(), 2); + var hour = zeroPad(dt.getHours(), 2); + var minute = zeroPad(dt.getMinutes(), 2); + var second = zeroPad(dt.getSeconds(), 2); + var millisecond = zeroPad(dt.getMilliseconds(), 3); + + return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + millisecond; }; SqlString.bufferToString = function(buffer) { @@ -124,8 +125,13 @@ SqlString.objectToValues = function(object, timeZone) { return values.join(', '); }; -function zeroPad(number) { - return (number < 10) ? '0' + number : number; +function zeroPad(number, length) { + number = number.toString(); + while (number.length < length) { + number = '0' + number; + } + + return number; } function convertTimezone(tz) {