1

i want to compare two dates and am using

new Date('december 2019')

and it is not working in react js / react native

        var resultList = [];
        var dated = new Date('december 2019');
        var endDated = new Date('march 2020');
        var monthNameList = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

        while (dated <= endDated)
        {
            console.log('checking');
            var stringDate = monthNameList[dated.getMonth()] + " " + dated.getFullYear() + " Qist is " + this.state.qistprice;
            resultList.push(stringDate);
            dated.setMonth(dated.getMonth()+1);
        }

in vanilla javascript it is working fine . but in react native it is not working if i try using format like that

new Date('december 13, 2019') then it will work but i dont want that i just want 'december 2019' to work .. is there any way around ?

code that i have tried in vanilla js is this

var resultList = [];
    var dated = new Date('december 2019');
    var endDated = new Date('march 2020');
    var monthNameList = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    while (dated <= endDated)
    {
      console.log('checking');
      var stringDate = monthNameList[dated.getMonth()] + " " + dated.getFullYear() ;
      resultList.push(stringDate);
      dated.setMonth(dated.getMonth()+1);
    }
    console.log(resultList)
deceze
  • 510,633
  • 85
  • 743
  • 889
samair ali
  • 772
  • 1
  • 5
  • 15
  • 2
    What *date* is "december 2019" supposed to be anyway? `Date` objects are specific *to the second*…! – deceze Jan 27 '20 at 12:51
  • Why don't you just use `new Date('december 01, 2019')`? It's the same – Sebastian Kaczmarek Jan 27 '20 at 12:53
  • @SebastianKaczmarek because it's similarly non-standard? – VLAZ Jan 27 '20 at 12:53
  • @VLAZ I know, it's another case. But OP asks for something else. I don't know his reasons nor project his working on. Maybe it's just how it has to be? Who knows? The result of `new Date('december 01, 2019')` will be the same – Sebastian Kaczmarek Jan 27 '20 at 12:55
  • @SebastianKaczmarek I'd avoid suggesting non-standard date formats. There are already enough problems with handling dates, no need to contribute to them. – VLAZ Jan 27 '20 at 12:56
  • @VLAZ Ok, I get your point. – Sebastian Kaczmarek Jan 27 '20 at 12:56
  • @samairali better to use a lib instead of handling this by hand example https://date-fns.org/ the `Date` implem depend on the runtime env and can give you some supprises – h1b9b Jan 27 '20 at 12:58
  • 1
    @samairali You still have to define *which day* of december you wish to use for your date object. If it doesn't matter, just use the first by adding the hard-coded "01" to your input syntax. – SparkFountain Jan 27 '20 at 12:58
  • @h1b9b basically the issue and the problem i want to understand is it is working perfectly in vanila js ... it s not working in react js only – samair ali Jan 27 '20 at 13:08
  • What exactly does it do differently in React? – deceze Jan 27 '20 at 13:22
  • @deceze it doesnot work , it doesnot create array as it is creating in vanila js code snippet that is have added ... – samair ali Jan 27 '20 at 13:23
  • Debug that in more depth. Is the `Date` object created different…? That would be surprising, but I'm not going to rule it out. You can create a snippet here including React to demonstrate such behaviour. – deceze Jan 27 '20 at 13:25
  • @deceze if you use the exact same code 100% exact same code as i have pasted in the snippet then it will not work .... it will not create an array the end result is not same ... – samair ali Jan 27 '20 at 13:26
  • FWIW, the above snippet doesn't even work as is in my browser… – deceze Jan 27 '20 at 13:30
  • http://prntscr.com/qti5gq you are not seeing it like that ? – samair ali Jan 27 '20 at 13:32
  • Nope. **Because you're asking the `Date` constructor to parse a _non-standard date_, and the result of that will depend on individual browsers.** – deceze Jan 27 '20 at 13:34
  • hmm so at the end of the day we can not achieve it as i want ? – samair ali Jan 27 '20 at 13:36
  • No, you can't just throw any random string at `Date` and get "what you mean" from it. – deceze Jan 27 '20 at 13:45
  • okay got it ... actually i was getting it in vanila js that is why i was confused ... – samair ali Jan 27 '20 at 13:47

2 Answers2

0

You can use moment.js library.

moment('december 2019').format('L')
"12/01/2019"

enter image description here

Ankit Kumar Rajpoot
  • 5,188
  • 2
  • 38
  • 32
0

Based on my experience, you should use momentjs (https://momentjs.com/) library to manipulate and handle the Date in your application. Native Date() object is really hard to manage and very off I had problems with it.

I can't help you now with native Date, but for a short answer, I recommend to use momentjs.