0

I have a date field in SharePoint list as mm/dd/yyyy

I want to list it as dd MonthName yyyy

When I use the below function, it return the date with one month added, for example, if the field date is 1/1/2019 it will be displayed as 1 February 2019 (instead of January, it displays February)

I tried to subtract 1 from monthindex but it didn't work.

function formatDate(thedate) {
  var monthNames = [
    "January", "February", "March",
    "April", "May", "June", "July",
    "August", "September", "October",
    "November", "December"
  ];
  var diffrentformat = thedate.split(" ")[0];
  var divideddate = diffrentformat.split("-");
  var d = new Date(divideddate[0], divideddate[1], divideddate[2]);
  //var d = new Date(thedate.split[]);
  var day = d.getDate();
  var monthIndex = d.getMonth();
  var year = d.getFullYear();

  return day + ' ' + monthNames[monthIndex] + ' ' + year;
}

I expect the output to be dd MonthName yyyy, but I'm getting dd nextMonthName yyyy

Jack Bashford
  • 38,499
  • 10
  • 36
  • 67
Shaka Zulo
  • 13
  • 2
  • Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – emix May 28 '19 at 10:02
  • It's really inefficient to parse a string to create a Date, then use date methods to get back the values you started with. Just dispense with the Date and use the values you've extracted from the string. – RobG May 28 '19 at 11:43

4 Answers4

0

In javascript the month start from 0-11.

So you should have to set the index as per that.

Vikash Pathak
  • 2,897
  • 1
  • 14
  • 28
0

Months are zero based (0 = January). You take that into consideration when building up the final string, but not when constructing the date object:

  new Date(
   /*year*/ divideddate [0] ,
   /*month*/ divideddate [1] - 1,
   /*day*/ divideddate[2]
  );
Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
0

You say that your date format is mm/dd/yyyy but your code looks like it's yyyy-mm-dd.

When you create a Date, you're not subtracting 1 from the calendar month:

var d = new Date(divideddate[0], divideddate[1], divideddate[2]);

should be:

var d = new Date(divideddate[0], divideddate[1] - 1, divideddate[2]);

However, you don't need to create a Date, it's simpler and less code to just use the values in the string:

// Reformat mm/dd/yyyy to d mmmm yyyy
function formatDate(thedate) {
  var monthNames = [
    "January", "February", "March",
    "April", "May", "June", "July",
    "August", "September", "October",
    "November", "December"
  ];
  var b = thedate.split(/\D/);
  return +b[1] + ' ' + monthNames[b[0]-1] + ' ' + b[2];
}

console.log(formatDate('5/28/2019'))

Note that the month array sets January as index 0, February as index 2, etc. so you can either subtract 1 from the month number in the string, or elide the first element in the array. I've done the former.

RobG
  • 124,520
  • 28
  • 153
  • 188
0

You can format and return using toLocaleDateString like below,

  return  day + ' ' + d.toLocaleDateString('en-us', { month: 'long' }) + ' ' + year;

No need to maintain month names in the list.

Muthu R
  • 766
  • 1
  • 9
  • 15