0

I'm trying to add a leading 0 before a certain part of a date. For example, if it's 9:00am, I want to display 09:00 and not 9:0. I want to be able to add a leading zero, so I can insert it into MySQL coding.

The result I'm getting is

2018-05-029 019:07:016

Here is my Javascript code:

 var login_date="";
                                                
    var d = new Date();
                                                
    var year            = d.getFullYear();
    var month           = d.getMonth()+1; /*months are from 0 - 11 */
    month               = '0' + month.toString().slice(-2);
    var day             = d.getDate();
    day                 = '0' + day.toString().slice(-2);
    var hour            = d.getHours();
    hour                = '0' + hour.toString().slice(-2);
    var minute          = d.getMinutes();
    minute              = '0' + minute.toString().slice(-2);
    var second          = d.getSeconds();
    second              = '0' + second.toString().slice(-2);
                                                         
    login_date = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    console.log(login_date);
Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
  • 3
    `('0' + stringifiedValue).slice(-2)` Slicing the result of concatenation. – PM 77-1 May 29 '18 at 18:21
  • Check this question out: https://stackoverflow.com/questions/5129624/convert-js-date-time-to-mysql-datetime I think you're overdoing it. There's a lot of pretty simple ways to cleanly get a MySQL formatted date from a JS Date. – dmgig May 29 '18 at 18:21
  • The comma ",slice" prints nothing. If there is no leading zero for single numbers, MySQL throws an error. –  May 29 '18 at 18:23
  • No, it needs to by MySQL format. The answers in the commas result in nothing being printed. –  May 29 '18 at 18:30
  • Any reason not to use `d.toISOString()`? – Tyler Roper May 29 '18 at 18:30
  • @KeithFeeney Perhaps I'm unclear about what you mean by "MySQL format". MySQL's `datetime` type is very flexible. For example, `UPDATE table_name SET datetime_field = '2018-05-29T18:23:10.552Z';` should work just fine. (Notice the date I'm using is the string returned by `.toISOString()`.) – Tyler Roper May 29 '18 at 18:32
  • My apologies, I meant DATETIME. The format that is used is YYYY-MM-DD HH:MM:SS . –  May 29 '18 at 18:35
  • @KeithFeeney If you run the query from my previous comment, it will insert into a MySQL `DATETIME` column as `2018-05-29 18:23:10`, just as you describe. However, perhaps you mean you want it to *display on the page* in that format? I'm simply saying that if you're going through all of this trouble solely to insert it into a MySQL database, none of this manipulation is necessary. – Tyler Roper May 29 '18 at 18:36

3 Answers3

2

You can check for the variable length of characters, if is less than two, then add a 0.

Something like this:

var d = new Date();

var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var hour =  d.getHours();
var minute =  d.getMinutes();
var second =  d.getSeconds();


if (month.toString().length < 2) month = '0' + month;
if (hour.toString().length < 2) hour = '0' + hour;
if (minute.toString().length < 2) minute = '0' + minute;
if (second.toString().length < 2) second = '0' + second;

console.log(year + '-' + month + '-' + day + " " + hour + ":" + minute + ":" + second)
Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
  • 1
    That's exactly what I wanted. Perfect. I dunno why I forgot about length. When I first saw your answer, I was like "Why didn't I think of that?". Thank you. –  May 29 '18 at 18:34
1

You could just check if the value is smaller then 10 to add an "0" at the beginning.

example

var seconds = seconds < 10 ? '0'+seconds : seconds;

Your final string could be defined like:

var login_date = year + "-" 
                + (month < 10 ? "0" + month : month) + "-" 
                + (day < 10 ? "0" + day : day) + " " 
                + (hour < 10 ? "0" + hour : hour) + ":" 
                + (minute < 10 ? "0" + minute : minute) + ":" 
                + (second < 10 ? "0" + second : second) ;
predkony
  • 101
  • 3
0

You can create a function addZero() that handles the concatenation of a 0 if necessary. Here is the code:

let addZero = (el) => ((el.toString().length == 1) ? '0' : '') + el.toString();

var login_date = "";

var d = new Date();

var year = d.getFullYear();
var month = d.getMonth() + 1; /*months are from 0 - 11 */
var day = d.getDate();
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();


login_date = year + "-" + addZero(month) + "-" + addZero(day) + " " + addZero(hour) + ":" + addZero(minute) + ":" + addZero(second);

document.write(login_date);
Ivan
  • 34,531
  • 8
  • 55
  • 100