在日期类型的输入中将当前日期设置为默认值

时间:2018-08-27 13:18:16

标签: html typescript materialize

我有一个类型为date的输入,在这里我使用物化来选择一个日期。我想将当前日期作为默认的init。

HTML

<input formControlName="invoice_date" id="invoice_date" type="date" class="datepicker" materialize="pickadate" [materializeParams]="[{ format: 'yyyy-mm-dd', formatSubmit: 'yyyy-mm-dd',
          closeOnSelect: true, selectMonths: true, selectYears: true, today: '',
          max: true, onSet: onSetDatepicker }]">

JavaScript

  onSetDatepicker(date) {
    if (date.select) {
      $('#invoice_date').pickadate().pickadate('picker').close();
    }
  }

3 个答案:

答案 0 :(得分:0)

datepicker libray通常将当前日期作为其默认起点,因此我不确定为什么您使用的日期有所不同。但是,您可以做的是将输入字段的value设置为今天的日期。

示例:

<input formControlName="invoice_date" id="invoice_date" type="date" class="datepicker" 
materialize="pickadate" [materializeParams]="[{ 
format: 'yyyy-mm-dd', formatSubmit: 'yyyy-mm-dd',
          closeOnSelect: true, selectMonths: true, selectYears: true, today: '',
          max: true, onSet: onSetDatepicker }]" 
value="<?php echo date('y-m-d'); ?>">

答案 1 :(得分:0)

也许这可以帮助您将日期的默认值设置为当前日期。

<input type="text" class="datepicker">
<script>
document.addEventListener('DOMContentLoaded', function () {
        let today = new Date().toLocaleDateString();
        var options = {
            defaultDate: new Date(today),
            setDefaultDate: true
        };
        var elems = document.querySelector('.datepicker');
        var instance = M.Datepicker.init(elems, options);
        instance.setDate(new Date(today));
});
</script>

How do I get the current date in JavaScript?

答案 2 :(得分:0)

05-12 23:29:44.274 14433-14433/? I/art: Late-enabling -Xcheck:jni
05-12 23:29:44.356 14433-14450/com.example.testdrei E/HAL: load: id=gralloc != hmi->id=gralloc
05-12 23:29:44.373 14433-14433/com.example.testdrei W/System: ClassLoader referenced unknown path: /data/app/com.example.testdrei-2/lib/arm64
05-12 23:29:44.401 14433-14433/com.example.testdrei I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
05-12 23:29:44.412 14433-14433/com.example.testdrei W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-12 23:29:44.477 14433-14433/com.example.testdrei I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
05-12 23:29:44.477 14433-14433/com.example.testdrei I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
05-12 23:29:44.559 14433-14433/com.example.testdrei W/ResourceType: Failure getting entry for 0x7f060061 (t=5 e=97) (error -75)
05-12 23:29:44.559 14433-14433/com.example.testdrei W/ResourceType: Failure getting entry for 0x7f060061 (t=5 e=97) (error -75)
05-12 23:29:44.592 14433-14433/com.example.testdrei I/Process: Sending signal. PID: 14433 SIG: 9

var date = new Date();

var day = ("0" + date.getDate()).slice(-2);
var month = ("0" + (date.getMonth() + 1)).slice(-2);

var today = date.getFullYear() + "-" + (month) + "-" + (day);

$('#dateid').val(today);