user6793735
user6793735

Reputation:

Javascript Slicing date/time with leading zero

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);

Upvotes: 0

Views: 4100

Answers (3)

Calvin Nunes
Calvin Nunes

Reputation: 6501

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)

Upvotes: 2

Ivan
Ivan

Reputation: 40778

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);

Upvotes: 0

predkony
predkony

Reputation: 101

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) ;

Upvotes: 1

Related Questions