1

This is what have tried:

partly pseudocode:

var hours = date1.getHours();
var minutes = date2.getMinutes();

if (hours.length == 1)
    hours = "0" + hours;

if (minutes.length == 1)
    minutes = "0" + minutes;

var time = hours + ':' + minutes;

Is there a smarter way like a formatted string function where I can say:

var minutes = date.getMinutes('mm');
var hours = date.getHours('hh');

so it adds the zeros automatically ?

Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • possible duplicate of [Javascript add leading zeroes to date](http://stackoverflow.com/questions/3605214/javascript-add-leading-zeroes-to-date) –  May 22 '13 at 14:04

6 Answers6

5

Here is your code fixed since there is no length on an integer

var hours = date1.getHours();
var minutes = date2.getMinutes();

if (hours<10) hours = "0" + hours;
if (minutes<10) minutes = "0" + minutes;

var time = ""+ hours + ":" + minutes;

You do not need a framework and there is no shorter way to do this

This may be what you mean:

Live demo

function pad(num) {
  return ("0"+num).slice(-2)
}
var time = pad(date1.getHours())+":"+pad(date2.getMinutes());
animuson
  • 53,861
  • 28
  • 137
  • 147
mplungjan
  • 169,008
  • 28
  • 173
  • 236
3

This functionality doesn't exist natively in javascript, you have to either add it yourself (as you have started to do), or, use a package.

Community
  • 1
  • 1
Jason
  • 15,915
  • 3
  • 48
  • 72
  • Love moments, a great and uncomplicated little date library. – Xotic750 May 22 '13 at 14:20
  • 1
    I did install moment.js now and got rid of 15 redundant lines of code replaced by 3 lines with better readability. I love moment from the very first moment... – Elisabeth May 23 '13 at 08:05
2

Use DateJS and you will be able to use mm and hh to add the preceding zeros :)

https://code.google.com/p/datejs/wiki/FormatSpecifiers

xShirase
  • 11,975
  • 4
  • 53
  • 85
  • Why the down vote? Some kind of conflict between Datejs and moment users? – xShirase May 22 '13 at 14:11
  • moment sounds much more l33t ;P – Elisabeth May 22 '13 at 14:27
  • 1
    Check this out : http://jsperf.com/underscore-date-vs-datejs/7 Quote : moment.js is lighter, and offers much faster parsing. On the other hand, date.js offers much faster formatting and manipulation. As the user is looking for formatting, I stick to my answer : DateJS rulez! – xShirase May 22 '13 at 14:30
  • 1
    its a shame that I have to use a library for such a common task... @xShirase If I would want speed I would not choose javascript ;-) – Elisabeth May 22 '13 at 19:06
1

You can add a method to Number prototype

  Number.prototype.pad0 = function(length) {
    var result = this.toString();
    while(result.length<length) result = "0"+result;
    return result;
  }

Then you can get what you want

  var date = new Date();  
  console.log(date.getMinutes().pad0(2));
  console.log(date.getHours().pad0(2));
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
1

Yet another way of doing it:

var d = new Date();
var t = [ d.getHours(), d.getMinutes(), d.getSeconds() ];

var s = t.map( function(z){return ('00'+z).slice(-2)} ).join(':');

console.log(s);

Time parts are put into an array. That array goes through map() where the numbers get leading zeros. The resulting array is then joined into a string with the ":" separator.

mivk
  • 13,452
  • 5
  • 76
  • 69
0

Convert numbers to strings before you check the lengths:

var hours = String(date1.getHours());
var minutes = String(date2.getMinutes());

if (hours.length == 1)
    hours = "0" + hours;

if (minutes.length == 1)
    minutes = "0" + minutes;

var time = hours + ':' + minutes;
kennebec
  • 102,654
  • 32
  • 106
  • 127