0

I am having date which is in mm/yyyy format. I need to compare this date with today's date. I have converted today's date.

After converting I have got date in mm/dd/yyyy format..But I need to covert it into mm/yyyy format..So that I can compare this date into, date which I am getting..Any help please.

selecteddate=05/2019  //which is in mm/yyyy format

myDate= new Date().toLocaleDateString(); //which is in mm/dd/yyyy format( I need to convert this date into mm/yyyy format and need to compare with selecteddate)
Peter O.
  • 32,158
  • 14
  • 82
  • 96
user11759028
  • 157
  • 1
  • 3
  • 13

5 Answers5

2

Using toLocaleDateString options

let date = new Date().toLocaleDateString('en-US', {year: 'numeric', month: '2-digit' })

console.log(date)
User863
  • 19,346
  • 2
  • 17
  • 41
1

Hope this helps,

const date = new Date();
const myDate = `${(date.getMonth() + 1)}/${date.getFullYear()}`;
console.log(myDate);
Maniraj Murugan
  • 8,868
  • 20
  • 67
  • 116
1

You can use Moment.js, as follows:

moment().format('MM/YYYY')
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Mustafa Kunwa
  • 821
  • 1
  • 9
  • 19
  • Moment is a very heavy package so there are other alternative ones. For example, date-fns. You can see a brief comparison in the repository: https://github.com/you-dont-need/You-Dont-Need-Momentjs – Aleksei Korkoza Dec 21 '19 at 07:02
  • moment is easier to use in comparison to other libraries available @AlexeyKorkoza but in the all upto you what you want to use – Mustafa Kunwa Dec 21 '19 at 07:27
  • I agree with you that the package is easy-to-use but you should think about performance and the size of your bundle as well. – Aleksei Korkoza Dec 21 '19 at 08:28
  • I agree with you that it's heavy,I tried to use date-fns I felt it was complicated so I choose to stay with moment @AlexeyKorkoza – Mustafa Kunwa Dec 21 '19 at 08:34
0
import datetime
selecteddate='05/2019'
selecteddate= datetime.datetime.strptime(selecteddate, '%m/%Y')
today_dtime= datetime.datetime.now()
today_dtime.date()>selecteddate.date()

output:True One thing note down month and year will assign as in selecteddate,But date will be 1st of respected month. try this simple trick

Imran_Say
  • 132
  • 10
0

You can use the following logic to compare your selected date with today's date

const selectedDate="05/2019"; // MM/YYYY
const today = new Date();
const todayString = `${(today.getMonth() + 1)}/${today.getFullYear()}`;
const matched = ( todayString === todayString );
console.log("Does your selected date matched with today's date ?",(matched ? "Matched":"Don't Matched"));
kuldipem
  • 1,719
  • 1
  • 19
  • 32
  • 1
    This won't work reliably as it doesn't pad single digit months, so in May will compare "05/202" with "5/2020". Also `( todayString === todayString )` will always return true (and the brackets are redundant). – RobG Dec 21 '19 at 08:54
  • Thanks, @RobG , to bring my attendance to that area. – kuldipem Dec 24 '19 at 05:44