0
Date.prototype.addDays = function(days) {
  var dat = new Date(this.valueOf());
  dat.setDate(dat.getDate() + days);
  return dat;
}

Date format for this code is Sun Mar 19 2017 16:17:11 GMT+0530 (India Standard Time)

How to change this format to Sun Mar 19 Also how to add suffix to date? (st, nd, rd, th)

1 Answer 1

4

You can use below function,

Date.prototype.addDays = function(days) {
  var dat = new Date(this.valueOf());
  locale = "en-us";
  dat.setDate(dat.getDate() + days);
  var curr_date = dat.getDate();
  var s=["th","st","nd","rd"];
  v= curr_date %100;
  var curr_month = dat.toLocaleString(locale, { month: "short" });
  return ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][dat.getDay()] +" " + curr_month + " " + curr_date +(s[(v-20)%10]||s[v]||s[0]);
}
1
  • Perfect. is it possible to add suffix to date? (st, nd, rd, th)
    – Doodu
    Mar 17, 2017 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.