1

I'm trying to compare 2 dates. If the date received is before the current day (Today) I have to delete the attribute @availabilityBeginDate but if the date received is after the current day I have to save it. I'm writing it in CoffeeScript.

I want to do that with Moment.js and diff. Here is the code I've tried but it doesn't work and I can't figure out why.

if (moment(moment(getCurrentDate()).diff(@availabilityBeginDate)).format("DD MMMM YYYY") < 0) 
    delete @availabilityBeginDate

Here is the getCurrentDate method :

getCurrentDate:() ->
  today = new Date
  dd = today.getDate()
  mm = today.getMonth() + 1
  yyyy = today.getFullYear()
  if dd < 10
    dd = '0' + dd
  if mm < 10
    mm = '0' + mm
  today = dd + ' ' + mm + ' ' + yyyy
  return today

Here is how I save @availabilityBeginDate :

bidDispoDetails.rows.push({label: 'Début', value: moment(@availabilityBeginDate).format("DD MMMM YYYY")}) if @availabilityBeginDate?
Nicolas Guerin
  • 356
  • 2
  • 18
  • Code delimiters should be used for actual code, not just highlighting words or phrases. – RobG Oct 23 '17 at 05:38
  • I don't get what you're asking @RobG – Nicolas Guerin Oct 23 '17 at 05:39
  • Not asking anything, just commenting. The *getCurrentDate* function returns a non-standard string that you're passing to moment.js without telling it the format. Likely there's an error being thrown that you aren't reporting. The first expression likely resolves to `'Invalid Date' < 0`. – RobG Oct 23 '17 at 05:43
  • by doing `.format("DD MM YYYY")` don't I set the format for both dates? Should I do `.format("DD MM YYYY")` for both dates or already in `getCurrentDate` ? – Nicolas Guerin Oct 23 '17 at 05:46
  • 1
    No, that sets an output format. The parse format is set in the call to *moment*: `moment(getCurrentDate(), 'DD MM YYYY')` but if the intention is to create a date for the start of the day, then `moment().startOf('day')` is far more efficient. – RobG Oct 23 '17 at 05:47

1 Answers1

3

The format method sets the format for output. To provide the format for parsing, supply it in the call to the constructor.

I think what you are looking for, in logical steps, is:

var availabilityBeginDate = '10 10 2017';
var format = 'DD MM YYYY';
var today = moment().startOf('day');
var begin = moment(availabilityBeginDate, format);

if (today.diff(begin) < 0) {
  console.log(today.format(format) + ' is  before ' +  begin.format(format));
  
} else {
  console.log(today.format(format) + ' is  after ' +  begin.format(format));

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>

You can munge all of that together if you want, the isAfter method is more semantic than diff:

var availabilityBeginDate = '10 10 2017';

if (moment().startOf('day').isAfter(moment(availabilityBeginDate, 'DD MM YYYY'))) {
  console.log('After');

} else {
  console.log('Before');

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>
RobG
  • 142,382
  • 31
  • 172
  • 209