Convert integer into future date with JavaScript

I have a MongoDB database connected to my web app. In it, I have the following Schema:

const SubmitSchema = new Schema ({
  date: String,

Date is stored as a string, but is actually an integer. These integers range from 1-31, so for example, the entries may be ‘1’, or ‘14’, or ‘26’.

What I want to do with this string integer, is the following:

  1. Pull the date from the database - complete
  2. Convert the date into an integer using parseInt - complete
  3. Turn the number into a date string - i.e, if the number in the database = 23, then turn the date string into (as a random example) - 23rd November 2020
  4. Look at today’s date, if before 23rd November 2020, keep the date as is, if todays date is the 23rd November 2020 or later, change it to 23rd December 2020

So, my question is - how do I do points 3 and 4. What is the best approach? I wish to do this with vanilla JavaScript in my React frontend.

I don’t want to store this date in my database… It seems overkill, just convert the string into that format for the user.

Any feedback would be appreciated.

I’m a little confused here. If the date string is just an integer between 1-31, then how can you turn it into a complete date with month and year?

Also, look at the Date object for manipulating dates in JS:

This is why I’m asking for help :sweat_smile:

It’s a tricky one, and quite a unique situation I think. So far I have the following code:

    const dayFromDB = parseInt("23", 10);
    const dayCurrent = new Date().getDate();
    const dayPreferred = Math.max(dayCurrent, dayFromDB);
    const dateToShow = new Date(new Date().setDate(dayPreferred));


This outputs the following:

Mon Nov 23 2020 16:15:17 GMT+0000 (GMT)

Which is reaaaaaally close. However, if I change the date to say, ‘13’, instead of ‘23’, it outputs…

Wed Nov 18 2020 16:17:14 GMT+0000 (GMT)

So what I need it to do is instead of getting today’s date, instead go across to December and show ‘23rd December 2020’. I’m not bothered about formatting for now, I’m happy with it just outputting like the date string above, it’s just this final step now…

Comparing and manipulating dates is relatively easy using the Date object. What I’m asking is, if you just have the integer 23, are you going to assume it is always for the current month and year? Can none of these “dates” be from previous months or years?

Sorry, my request was really unclear.

Basically, the date I’m trying to set can never be in the past.

I want to follow the following criteria in setting the date:

  • If today’s date is less than Date stored in database, then the date rendered will be the Date in database for the current month.

So what I mean here is, say I have ‘23’ stored in my database. Because today’s date is the 18th, the date I would want to display, since it’s November, is November 23rd 2020.

  • However, if today’s date is greater than, or equal to, the date stored in the database, then I want to display next month instead.

So here, say today’s date is the 23rd November (my favourite day apparently). But the ‘date’ stored in my database is ‘18’. Because we’re past the 18th November 2020, the date I would now want to display is the 23rd December 2020.

Similarly, say today’s date was the 23rd November. And say the date in the database was ‘23’. The date I would want to display is in fact the 23rd December, because we’re on ‘todays’ date.

Then, when we get to the 23rd December or later, I’d want the date to display as ‘23rd January 2021’. And so on, and so on.

EDIT: And obviously, in between these dates, so from the 24th November to the 22nd December, it would show ‘23rd December 2020’.

Hope that makes sense?

OK, I think I understand now.

const dateToShow = new Date(new Date().setDate(dayPreferred));

setDate() only sets the day of the month. If you don’t set the month or year as well then it defaults to the current month/year. So if you want to set it to the next month then you have to explicitly do that.

Thank you for the response. I really appreciate it.

I’m struggling with Date in JavaScript honestly. Would you be able to provide an example so I can visualise fully what you mean?

When you create a new Date() it is initialized with the current date. The setDate() method only changes the ‘day’ of the current date. There is a setMonth() method which allows you to change the current month. But then what if you are in December? The next month would be in the next year and so you would have to use setYear() to change the year as well. And what if the current month has 31 days and the next month only has 30, or 29, or 28? It’s not quite as simple as just changing the month to the next month. Do a google search for “javascript add months to date” and you’ll see all the edge cases you’ll need to take into account.

It really depends on how precise you want to be with this. My recommendation would be to use a third-party date library, as they have done all the complicated stuff for you :slight_smile: A few libraries I would look at:


One of the first and most popular of these libraries is moment.js ( but some think it has run its course and the ones listed above may be better. Even their own website encourages you to use something else.