2

How can I get the date and time in javascript as 12/08/2015-1:49? I tried the following but I get an error TypeError: now.format is not a function

var now = new Date();
now.format("dd/mm/yy-h:mm tt");
console.log(now); //TypeError: now.format is not a function
Becky
  • 5,467
  • 9
  • 40
  • 73
  • 1
    What make you think that there is a `format` method ? [mdn docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/prototype) – Hacketo Aug 12 '15 at 11:23
  • 3
    possible duplicate of [Where can I find documentation on formatting a date in JavaScript?](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – Hacketo Aug 12 '15 at 11:25
  • @Hacketo thanks. [This is where I got that](http://stackoverflow.com/questions/4744299/how-to-get-datetime-in-javascript) – Becky Aug 12 '15 at 11:25
  • 2
    this post is talking about dependency that you need to download and include in your project – Hacketo Aug 12 '15 at 11:26
  • 2
    and the dependency in question modifies Date.prototype, this is generally considered bad (read: awful) practice. – Jared Smith Aug 12 '15 at 11:27

6 Answers6

3

There is no any format method for Date in JavaScript. Either you need to use any other external libraries like momentjs, or write your own script to format.

Here is example how you can convert date to dd/mm/yy-h:mm tt format

    var now = new Date();

    var date = now.getDate() + "/" + (now.getMonth() + 1) + "/" + now.getFullYear() + "-" + now.getHours() + ":" + now.getMinutes() + " " + (now.getHours() > 12 ? "PM" : "AM");

    console.log(date)
Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65
1

Try this:

function getFormattedDate() {
    var date = new Date();
    var str = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getYear() + "-" +  date.getHours() + ":" + date.getMinutes() + " " + date.getSeconds();
    return str;
}
Branimir Đurek
  • 632
  • 5
  • 13
1

Extend Date`s prototype, add function format

  Date.prototype.format = function(format){
    format = format || "Y/M/D H:I:S";
    var data = {
      y: this.getFullYear() % 100,
      Y: this.getFullYear(),
      m: this.getMonth() + 1,
      d: this.getDate(),
      h: this.getHours(),
      i: this.getMinutes(),
      s: this.getSeconds()
    };
    var needAddZeroLTTen = "mdhis".split('');
    for(var i = 0; i < needAddZeroLTTen.length; i ++){
      var prop = needAddZeroLTTen[i];
      data[prop.toUpperCase()] = data[prop] < 10 ? ('0' + data[prop]) : data[prop];
    }
    var dateStr = format;
    for(var i in data){
      var reg = new RegExp(i,'g');
      dateStr = dateStr.replace(reg, data[i]);
    }
    return dateStr;
  }

Then use below code to format a date

var date = new Date();
var dateStr = date.format('D/M/y-h:I');
Abot Chen
  • 21
  • 1
0

the best way to manage dates in js is using http://momentjs.com/ here you will find a great way to format the dates

lem2802
  • 1,152
  • 7
  • 18
0

You can either

  • do this by hand by using the functions on Date like date.getMonth(), however these do not support zero padding, and it gets quite fiddly. Only do this if you cannot include a third-party library, you're obsessive about load time / performance or you really enjoy re-inventing the wheel.

  • Use a third-party library like moment, this has multiple formats and supports padding, e.g. MM will force month as two characters.

Example

var now = new Date();
console.log(moment(now).format("DD/MM/YY-hh:mm Z"));
Adam
  • 35,919
  • 9
  • 100
  • 137
-1

Moment.JS would help you. Please take a look on this JSFiddle: http://jsfiddle.net/f3zp5zuv/

alert (moment('2015 Apr 30').format('DD/MM/YY -h:mm'))

Moment: http://momentjs.com/docs/#/displaying/

alert (moment('2015 Apr 30 14:42:00').format('DD/MM/YY -h:mm'))
<script src="http://momentjs.com/downloads/moment.js"></script>
Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117