0

I got this piece of code:

    xtype: 'datefield',
    editable: false,
    itemId: 'start-date-field',
    fieldLabel: 'Från',
    labelWidth: 50,
    format: 'Y-m-d',
    value: new Date(),
    disabled: true

and I'm trying to subtract some days from "new Date()" but my googlefu isn't good enought and this is probably easy enough for most people. I've tried new Date().getDay - 30 but that only generates an error.

President Camacho
  • 1,860
  • 6
  • 27
  • 44

1 Answers1

1
xtype: 'datefield',
editable: false,
itemId: 'start-date-field',
fieldLabel: 'Från',
labelWidth: 50,
format: 'Y-m-d',
value: (function() {var lastMonth = new Date(); return lastMonth.setDate(lastMonth.getDate()-30), lastMonth;})(),
disabled: true

JSFiddle

laruiss
  • 3,780
  • 1
  • 18
  • 29
  • Thanks it works, but why can't I use my solution that I suggested above in the comments? – President Camacho Oct 09 '14 at 14:20
  • Well, because `new Date().getDate()+1` is parsed as `(new Date()).getDate()+1`, whereas `new Date().getDate()-1` is parsed as `(new Date().getDate())-1`. Not really sure why, though. – laruiss Oct 09 '14 at 14:25
  • So should it work if i type in (new Date()).getDate()-1 instead without the compiler parsing it? Or will the compiler just compile it further? – President Camacho Oct 09 '14 at 14:30
  • Actually, it does not matter, you will always get the date (i.e. the day of the month) with Date#getDate(); – laruiss Oct 09 '14 at 14:37