0

My current date format is 30-Dec-2018. I want to add 2 month (might change) to this date format so my output will be 28-Feb-2019. I have given few tries to this but haven't succeeded.

I think my date format is causing issue in generating correct output. Any help in adding a month to the above mentioned date format in javascript. Thanks

P.S: Adding date might change the year as well

EDIT:

var date= new Date(30-Dec-2018);
var month_add  = date.setMonth(date.getMonth()+2);
    alert(month_add);

output I am getting in alertbox is: 1553022000000

Muhammad Asif Raza
  • 427
  • 1
  • 4
  • 18
  • 1
    the format need not be an issue. Parse it into a `Date` object and then the format is irrelevant. You can do your calculations. Then if you need to display it again, output it in the right format. Specific formats are only for humans. Computers don't use them or care about them when processing dates. – ADyson Feb 19 '19 at 20:06
  • 3
    Anyway it's unclear why the solutions in the question you linked to didn't work for you. Perhaps if you post the code you tried, we can help you to fix it. – ADyson Feb 19 '19 at 20:07
  • 1
    Have you looked into a DateTime library like [moment.js](https://momentjs.com/)? – Abion47 Feb 19 '19 at 20:08
  • 2
    How is Feb 28 two months from Dec 30? What is your definition of a month? – Paul Feb 19 '19 at 20:12
  • We need a [mcve] ! – Jonas Wilms Feb 19 '19 at 20:14
  • The code as shown doesn't work. Please take your time and fix the code, provide a [mcve]! – Jonas Wilms Feb 19 '19 at 20:16
  • @ADyson I have added the code and my output – Muhammad Asif Raza Feb 19 '19 at 20:16
  • 1
    I wrote a snippet to parse your date format for you, but when I add two months to that date I get `March 2`, Two months from Dec 30 seems it should be two days after Feb 28, which indeed is March 2. – Paul Feb 19 '19 at 20:16
  • @Paulpro you are right about 2 months. Can you share snipet? – Muhammad Asif Raza Feb 19 '19 at 20:18
  • 1
    The return value of the `Date.setMonth()` function is the amount of milliseconds between `1 January 1970 00:00:00 UTC` and the new date (aka a UNIX timestamp). Perhaps you want to alert `date` instead, which is the modified date object (in the format of a date, rather than UNIX) . That said, your code doesn't work because your original date is invalid. – Tyler Roper Feb 19 '19 at 20:20
  • Sure, I posted an answer. Please note that I recommend always using the ISO 8601 date format (2018-12-30) for several reasons: 1) Supports leap seconds 2) Supports timezone information 3) Supports milliseconds, or more broadly any amount of precision desired. 4) Lexicographically sorting ISO date strings that are using the same timezone is the same as chronologically sorting the dates. – Paul Feb 19 '19 at 20:37

1 Answers1

0

You could parse that date string into year, month_index, and day components so that you can use the Date constructor with them, add the two months using Date methods, and then format it back into a string:

const input = '30-Dec-2018';

console.log( add_two_months( input ) );

function add_two_months ( input ) {
  const date = parse_date_string( input ); 
  date.setMonth( date.getMonth( ) + 2 );
  return format_date( date );
}

// Expects input in form '30-Dec-2018'
function parse_date_string ( str ) { 
  const [ , day, month_str, year ] = input.match( /(\d*)-(\w*)-(\d*)/ );

  const month_index = { 
    jan: 0,
    feb: 1,
    mar: 2,
    apr: 3,
    may: 4,
    jun: 5,
    jul: 6,
    aug: 7,
    sep: 8,
    oct: 9,
    nov: 10,
    dec: 11,
  }[ month_str.toLowerCase( ) ];

  return new Date( year, month_index, day );
}

function format_date ( date ) {
  const day = date.getDate( ).toString( ).padStart( 2, '0' );
  
  const month = [
    'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
  ][ date.getMonth( ) ];
  
  const year = date.getFullYear( );
  
  return `${day}-${month}-${year}`;
}

If you used the ISO 8601 date/time format, which I highly recommend for several reasons, this could all be reduced to:

const input = '2018-12-30';

console.log( add_two_months( input ) );

function add_two_months ( input ) {
  const date = new Date( input );
  date.setMonth( date.getMonth( ) + 2 );
  return date.toISOString( ).substr( 0, 10 );
}
Paul
  • 130,653
  • 24
  • 259
  • 248