How to set datetime on datetime-local via jQuery

前端 未结 7 2028
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-18 15:43

I have datetime-local html control on my form and I need to set date and time on it dynamically via JS or jQuery. How can I do it?



        
7条回答
  •  爱一瞬间的悲伤
    2021-02-18 16:10

    I wrote a jQuery method that sets the current UTC time. It's useful for initializing datetime and datetime-local input fields.

    Here's how you would use it to initialize a field.

    $('input[type="datetime"]').setNow();
    

    Or pass an argument of true to only set fields with no value.

    $('input[type="datetime"]').setNow(true);
    

提交回复
热议问题