0
var d = new Date();
var month = d.getMonth();
var monthday = d.getDate();
var hours = d.getHours();
var minute = d.getMinutes();
var date = document.getElementsByClassName("date");

date[0].innerHTML = [month + 1] + "." + monthday + " " + hours + ":" + minute;

This is printing the current date like this: 9.1 3:31 ... and I want to format like this: 09.01 03:31 Any idea how can I do that in the shortest code?

Daniel_Knights
  • 7,940
  • 4
  • 21
  • 49
Dobee dsa
  • 33
  • 4

9 Answers9

1

Add for every value before the string "0" and get with slice(-2) the last 2 chars.

var d = new Date();
var month = d.getMonth();
var monthday = d.getDate();
var hours = d.getHours();
var minute = d.getMinutes();

var date = document.getElementsByClassName("date");

let res = '0'+[month + 1].slice(-2) + "." + ('0'+monthday).slice(-2) + ". " + ('0'+hours).slice(-2) + ":" + ('0'+minute).slice(-2) ;

console.log(res);
Sascha
  • 4,576
  • 3
  • 13
  • 34
0

You can use padStart method:

 date[0].innerHTML = String([month + 1]).padStart(2, '0') + "." + String(monthday).padStart(2, '0') + " " + hour + ":" + minute ;
AbbasEbadian
  • 653
  • 5
  • 15
0

Try this

    var d = new Date();
    var month = d.getMonth();
    var monthday = d.getDate();
    var hours = d.getHours();
    var minute = d.getMinutes();

    var date = document.getElementsByClassName("date");

    function pad(s) { return (s < 10) ? '0' + s : s; }

    date[0].innerHTML = pad([month + 1]) + "." + pad(monthday) + " " + pad(hour) + ":" + pad(minute);

Ludovic
  • 501
  • 5
  • 10
0

Here is simple solution you can create pad to do so

Example

var d = new Date();
var month = d.getMonth();
var monthday = d.getDate();
var hours = d.getHours();
var minute = d.getMinutes();
var date = document.getElementsByClassName("date");
var pad = "00";
var n = `${monthday}`;
date[0].innerHTML = [month + 1] + "." + (pad + n).slice(-pad.length) + " " + hours + ":" + minute;
<div class="date"></div>
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
0

You should try this

var d = new Date();
    var month = d.getMonth();
    var monthday = d.getDate();
    var hours = d.getHours();
    var minute = d.getMinutes();

    var date = document.getElementsByClassName("date");
    let format= (number) => {
        if(number<10) {   
           return 0 + number.toString();
        }
        return number;
    }
    date[0].innerHTML = format(month + 1) + "." + format(monthday) + " " + format(hour) + ":" + format(minute);
Eazash
  • 129
  • 1
  • 8
0

You can use simple function for this particular case:

function formatNumber(num) { return num < 10 ? '0' + num : num; } 

Like this:

console.log(formatNumber([month + 1]) + "." + formatNumber(monthday) + " " + formatNumber(hours) + ":" + formatNumber(minute));
Aleksei Golubev
  • 342
  • 3
  • 11
0

if u are using angular make use of the date pipe

var d = new Date();
    

in your html

{{d | date:'medium'}}
Banny
  • 111
  • 1
  • 6
0

I like to use toLocaleDateString and toLocaleTimeString or you can use toLocaleString

var date = new Date();
console.log("toLocaleDateString and  toLocaleTimeString example result: ");
console.log(     
  date.toLocaleDateString('en-GB', {
      month: '2-digit',
      day: '2-digit'
  }).replace(/\//g, '.') + " " + date.toLocaleTimeString('en-GB', {
      hour: '2-digit',
      minute: '2-digit'
  })
);
console.log("toLocaleString example result: ");
console.log( 
  date.toLocaleString('en-GB', {
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
  }).replace(/\//g, '.').replace(/,/g, '')
);
angel.bonev
  • 2,154
  • 3
  • 20
  • 30
0

If you are okay with an external lib for this, do check out moment.js. This provides you with greater flexibility when you want to play with different date/time formats.

Solving your problem the moment way (though appreciate other posted raw solutions):

console.log(moment().format('MM.DD hh:mm')); // "09.01 06:22"
        
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>  
angel.bonev
  • 2,154
  • 3
  • 20
  • 30
Abhijeet_IXR
  • 807
  • 6
  • 15