admin管理员组

文章数量:1327849

in database the created date is like "2018-12-01 18:51:41".Based on this date i want to pare this date with current month.If current month is not equal to created date just show alert "not in this month".

I tried this code so far

const dateTime = "2018-09-01 18:51:41";
const parts    = dateTime.split(/[- :]/);

var month = parts[1];
var day   = parts[2]; 
var year  = parts[0];

var currentdate = new Date();
var cur_month   = currentdate.getMonth() + 1;
var cur_day     = currentdate.getDate();
var cur_year    = currentdate.getFullYear();

if(cur_month==month && day >= cur_day)
{
    alert("in this month");
}
else
{
    alert("not in this month");
}

Can anyone help me plz?

in database the created date is like "2018-12-01 18:51:41".Based on this date i want to pare this date with current month.If current month is not equal to created date just show alert "not in this month".

I tried this code so far

const dateTime = "2018-09-01 18:51:41";
const parts    = dateTime.split(/[- :]/);

var month = parts[1];
var day   = parts[2]; 
var year  = parts[0];

var currentdate = new Date();
var cur_month   = currentdate.getMonth() + 1;
var cur_day     = currentdate.getDate();
var cur_year    = currentdate.getFullYear();

if(cur_month==month && day >= cur_day)
{
    alert("in this month");
}
else
{
    alert("not in this month");
}

Can anyone help me plz?

Share Improve this question edited Dec 6, 2018 at 4:15 becool asked Dec 6, 2018 at 3:29 becoolbecool 812 gold badges3 silver badges11 bronze badges 4
  • 1 day should be parts[0] and year should be parts[2]? – Thum Choon Tat Commented Dec 6, 2018 at 3:33
  • I tried the same script for todays date it seems working in chrome plnkr.co/edit/M9TLNOe4zeLxbvp3dJm2?p=preview – Siva Karthikeyan Commented Dec 6, 2018 at 3:34
  • Your code working fine. – Shubham Baranwal Commented Dec 6, 2018 at 3:36
  • i have updated my code plz check it – becool Commented Dec 6, 2018 at 3:38
Add a ment  | 

2 Answers 2

Reset to default 4

You only need to check with month and year value. Don't consider date in your condition. Have a look to my code -

const dateTime = "2018-11-01 18:51:41";
const parts = dateTime.split(/[- :]/);

var month = parts[1];
var year = parts[0];

var currentdate = new Date();
var cur_month = currentdate.getMonth() + 1;
var cur_year = currentdate.getFullYear();

if (cur_month == month && year == cur_year) {
  alert("in this month");
} else {
  alert("not in this month");
}

You only need to check that currentMonth === dateMonth && currentYear === dateYear. If they are then you are in the current month and don't need to check the day.

本文标签: jqueryHow to check given date is equal to current month using javascriptStack Overflow