最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript datetime add zero to single digit values - Stack Overflow

programmeradmin3浏览0评论

i have this way of getting tomorrows date.

var tomorrow = date.getDate() + "/" + month + "/" + date.getFullYear();

this returns 2/10/2013 where as i want it to be 02/10/2013 (with the zero for single digits)

This is needed to do a date parison.

if(02/10/2013==2/10/2013){
dosomething();
}

The above doesnt work due to that issue.

i have this way of getting tomorrows date.

var tomorrow = date.getDate() + "/" + month + "/" + date.getFullYear();

this returns 2/10/2013 where as i want it to be 02/10/2013 (with the zero for single digits)

This is needed to do a date parison.

if(02/10/2013==2/10/2013){
dosomething();
}

The above doesnt work due to that issue.

Share Improve this question asked Oct 1, 2013 at 11:05 dev1234dev1234 5,72616 gold badges61 silver badges118 bronze badges 8
  • 2 If you concatinate a character to a number or a date value, the result will be changed into a string. Although your syntax is wrong, the parison will ever fail. – Reporter Commented Oct 1, 2013 at 11:07
  • @reporter can you be more specific? is it a possible thing to do or nor ? – dev1234 Commented Oct 1, 2013 at 11:09
  • 1 I assume you want 0 added to months too. – Andy Commented Oct 1, 2013 at 11:09
  • 1 @mazraara: I think you are looking for something like this. Correct? – Harry Commented Oct 1, 2013 at 11:21
  • 2 @mazraara If you are satisfied then accept any one answer – Swapnil Patil Commented Oct 1, 2013 at 12:05
 |  Show 3 more ments

4 Answers 4

Reset to default 4

You can do by

var tomorrow = ('0' + date.getDate()).slice(-2) + "/" +  ('0' + (date.getMonth()+1)).slice(-2) + "/" + date.getFullYear();

why you don't use str_pad from phpjs this is the code :

  function str_pad (input, pad_length, pad_string, pad_type) {
  // http://kevin.vanzonneveld
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld)
  // + namespaced by: Michael White (http://getsprink.)
  // +      input by: Marco van Oort
  // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
  // *     returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
  // *     example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
  // *     returns 2: '------Kevin van Zonneveld-----'
  var half = '',
    pad_to_go;

  var str_pad_repeater = function (s, len) {
    var collect = '',
      i;

    while (collect.length < len) {
      collect += s;
    }
    collect = collect.substr(0, len);

    return collect;
  };

  input += '';
  pad_string = pad_string !== undefined ? pad_string : ' ';

  if (pad_type !== 'STR_PAD_LEFT' && pad_type !== 'STR_PAD_RIGHT' && pad_type !== 'STR_PAD_BOTH') {
    pad_type = 'STR_PAD_RIGHT';
  }
  if ((pad_to_go = pad_length - input.length) > 0) {
    if (pad_type === 'STR_PAD_LEFT') {
      input = str_pad_repeater(pad_string, pad_to_go) + input;
    } else if (pad_type === 'STR_PAD_RIGHT') {
      input = input + str_pad_repeater(pad_string, pad_to_go);
    } else if (pad_type === 'STR_PAD_BOTH') {
      half = str_pad_repeater(pad_string, Math.ceil(pad_to_go / 2));
      input = half + input + half;
      input = input.substr(0, pad_length);
    }
  }

  return input;
}

and apply it to your code:

var tomorrow = str_pad(date.getDate(),2,0,'STR_PAD_LEFT') + "/" + str_pad(month,2,0,'STR_PAD_LEFT') + "/" + date.getFullYear();

Try this:

if(new Date(2013,2,10).toString() == new Date(2013,02,10).toString()){
    dosomething();
}
$.datepicker.formatDate('dd/mm/yy', tomorrow);

You could also use moment.js library for time/date manipulation

发布评论

评论列表(0)

  1. 暂无评论