Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Moment JS today's date validation issue with Vue

In my VueJS application I have a component with a form.

In that form I have a field to pick the date.

My requirement is to show an error message if the selected date is older than the current date.

Basically the selected date need to be either today's date or future date.

I'm using Moment JS.

I have following custom rule in my Validator.vue

const dateIsToday = (value) => {
    var todayDate = moment(new Date()).format("DD-MM-YYYY");
    var selectedDate = value;
    return selectedDate>=todayDate;
};

But this works only if I selected an old date from the current month... Assume if the user has picked an older date from this month like 10-04-2022, then it'll show the error message.

But if the user selected an old date from last month or a past month like 10-01-2022, this won't show me the error message....

In my form.vue I have

<div class="w-1/2 mr-2">
                        <p class="text-certstyle-titles font-bold text-sm mb-1">Start date</p>
                        <div class="h-12">
                            <cs-date-picker
                                id="startDate"
                                v-model="project.start_date"
                                :default-selection="true"
                                :name="`${identifier}-start_at`">
                            </cs-date-picker>
                            <validator
                                :identifier="`${identifier}-validate-project`"
                                :rules="validations.startDate"
                                :name="`${identifier}-start_at`"
                            />
                        </div>
                    </div>

And under my validations I have,

startDate: {
                    required: {
                        message: 'Project Start date is required.'
                    },
                    dateIsToday: {
                        message: 'Date has to be today's date or a future date.'
                    },
                },
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It seems that you are comparing strings. Instead you should make real use of moment and compare moment dates:

const dateIsToday = (value) => {
    let todayDate = moment();
    let selectedDate = moment(value, "DD-MM-YYYY");
    return selectedDate.isSameOrAfter(todayDate);
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!