Becky
Becky

Reputation: 5585

Date and time in javascript

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

Upvotes: 2

Views: 361

Answers (6)

Laxmikant Dange
Laxmikant Dange

Reputation: 7688

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)

Upvotes: 3

Abot Chen
Abot Chen

Reputation: 21

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

Upvotes: 1

Adam
Adam

Reputation: 36703

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

Upvotes: 0

lem2802
lem2802

Reputation: 1162

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

Upvotes: 0

Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24590

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>

Upvotes: -1

Branimir Đurek
Branimir Đurek

Reputation: 632

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

Upvotes: 1

Related Questions