1

I have input date field and I want to prefilled it with current year only, so that whenever user want to update that field, he/she don't have to specify year each time, the field always prefilled with current year and user will edit date and month. If user want to edit year it is possible.

<input type="date" />

It is about year only, what i want to do is when specifically editing with keyboard, user shouldn't type year everytime, because in most of the cases the year will be current year.

Faizan
  • 1,132
  • 2
  • 12
  • 23
  • 1
    I think you will have to split your date in 2 or 3 separate fields to do that without an headache. To prefill with current year, you need to fill it in javascript with something like new Date().getYear() – Kleioz Dec 04 '17 at 08:58
  • 1
    Possible duplicate of [How to set default value to the input\[type="date"\]](https://stackoverflow.com/questions/14212527/how-to-set-default-value-to-the-inputtype-date) – empiric Dec 04 '17 at 08:58
  • Set today's date as the value, and it will automatically open for current month. – 31piy Dec 04 '17 at 09:04
  • @empiric, my question is about year only. – Faizan Dec 04 '17 at 10:23
  • @Kleioz good option this – Faizan Dec 04 '17 at 10:24
  • @31piy, it is already opening current month calendar, what i want to do is when specifically editing with keyboard, user shouldn't type year everytime, because in most of the cases the year will be current year. – Faizan Dec 04 '17 at 10:28

2 Answers2

1

Best i could come up with is setting both min and max date. That way browser knows year can not be changed and are therefore prefilled.

<input type="date" min="2017-01-01" max="2017-12-31">

skarmavbild 2017-12-04 kl 11 44 52

But then you are not able to change the year which is not what you want...

Otherwise I don't think this would be possible. But maybe you shouldn't bother so much. ppl might click the arrow button to select date anyway. that is only two clicks away, the current year will be selected when clicking on a date. An alternativ is to click opt/alt + down key on chrome for Mac to get the date picker.

Endless
  • 34,080
  • 13
  • 108
  • 131
-2

use following code to manage it

var date = new Date();
var fullYear = date.getFullYear();
$("input[type='date']").val(fullYear);
empiric
  • 7,825
  • 7
  • 37
  • 48