Skip to content Skip to sidebar Skip to footer

Get Date/timestamp In A Particular Format

I want to get Date in the for following format 16-SEP-16 12.00 in javascript. I can't quite figure out how to achieve this. I need to send this as a query parameter for a REST API

Solution 1:

If you dont have any problem using third party library, you can use Moment.js,it is a beautiful library for dates and formatting

In moment.js,you can format it as the following

var day = moment("2016-11-01");
console.log(day);
console.log(day.format("DD-MMM-YYYY HH:MM"));
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.js"></script>

Hope this helps

Solution 2:

var monthNames = [
  "Jan", "Feb", "Mar",
  "Apr", "May", "Jun", "Jul",
  "Aug", "Sep", "Oct",
  "Nov", "Dec"
];


var date = newDate();
var dt = date.getUTCDate() + '-' + monthNames[date.getUTCMonth()] + '-' + (date.getUTCFullYear() - 2000) + ' ' + date.getUTCHours() + '.' + date.getUTCMinutes();
console.log(dt);

without using month array you can use below solution

var date = newDate();
Date.prototype.monthName = function() {
    returnthis.toUTCString().split(' ')[2]
};
var dt = date.getUTCDate() + '-' + date.monthName()+ '-' + (date.getUTCFullYear() - 2000) + ' ' + date.getUTCHours() + '.' + date.getUTCMinutes();
console.log(dt);
document.write(dt);

Solution 3:

Insomnia made me write this..

It adds a format method to the date object without requiring any 3rd party libraries and returns a formatted date from the given shorthand.

I based it off PHP's date shorthand, which you can see for reference.

Date.prototype.format = function(format) {
  var months = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
  ];
  var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
  format = format.replace(/y/g, ("" + this.getFullYear()).substring(2));
  format = format.replace(/Y/g, "" + this.getFullYear());
  format = format.replace(/m/g, ("00" + (this.getMonth() + 1)).substr(-2, 2));
  format = format.replace(/F/g, months[this.getMonth()]);
  format = format.replace(/M/g, months[this.getMonth()].substring(0, 3));
  format = format.replace(/n/g, "" + (this.getMonth() + 1));
  format = format.replace(/t/g, "" + newDate(this.getFullYear(), this.getMonth() - 1, 0).getDate());
  format = format.replace(/D/g, days[this.getDate()].substr(0, 3));
  format = format.replace(/d/g, ("00" + this.getDate()).substr(-2, 2));
  format = format.replace(/j/g, this.getDate()+"");
  format = format.replace(/l/g, days[this.getDate()]);
  format = format.replace(/w/g, this.getDay());
  format = format.replace(/a/g, this.getHours() > 11 ? "pm" : "am");
  format = format.replace(/A/g, this.getHours() > 11 ? "PM" : "AM");
  format = format.replace(/g/g, "" + (this.getHours() > 11 ? this.getHours() - 11 : this.getHours() + 1));
  format = format.replace(/G/g, "" + (this.getHours() + 1));
  format = format.replace(/h/g, ("00" + (this.getHours() > 11 ? this.getHours() - 11 : this.getHours() + 1)).substr(-2, 2));
  format = format.replace(/H/g, ("00" + (this.getHours() + 1)).substr(-2, 2));
  format = format.replace(/i/g, ("00" + this.getMinutes()).substr(-2, 2));
  format = format.replace(/s/g, ("00" + this.getSeconds()).substr(-2, 2));
  return format;
};

Examples..

// 11/02/16 2:17 AMvar d = (newDate()).format("m/d/y g:i A");

// November 2, 2016, 2:17 amvar d = (newDate()).format("F j, Y, g:i a")

And a fiddle

Post a Comment for "Get Date/timestamp In A Particular Format"