English 中文(简体)
投入类型日期
原标题:Set date in input type date

今天,我将在日期皮尔斯输入台风日期 Chrome中确定日期。

$(document).ready(function() {
    let now = new Date();
    let today = now.getDate()  +  /  + (now.getMonth() + 1) +  /  + now.getFullYear();
    console.log(today);
    $( #datePicker ).val(today);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="datePicker" type="date">

然而,它没有发挥作用。

请审判 Chrome号。

最佳回答
问题回答
var today = new Date().toISOString().split( T )[0];
$("#datePicker").val(today);

首先是法典。

最新信息:我这样做是为了:

date.toISOString().substr(0, 10)

其结果与接受的答复相同,并得到良好的支持。

对我来说,解决这一问题的最短途径是利用时间。 仅用两条线来处理这个问题。

var today = moment().format( YYYY-MM-DD );
$( #datePicker ).val(today);

我通常在利用日期投入时设立这两个助手职能:

// date is expected to be a date object (e.g., new Date())
const dateToInput = date =>
  `${date.getFullYear()
  }-${( 0  + (date.getMonth() + 1)).slice(-2)
  }-${( 0  + date.getDate()).slice(-2)
  }`;

// str is expected in yyyy-mm-dd format (e.g., "2017-03-14")
const inputToDate = str => new Date(str.split( - ));

之后,你可以确定日期投入价值如下:

$( #datePicker ).val(dateToInput(new Date()));

And retrieve the selected value like so

const dateVal = inputToDate($( #datePicker ).val())

您只能使用输入<条码>类型=”<<>t>>>>>/code>,作为格式<条码>。 YYYY-MM-DD

我已在NODE.js 露头条栏中作为<代码>formatDate实施帮助,不需要担心......像第一行所述那样使用。

e.g:

< input type="date" id="date" name="date" class="form-control" value="{{formatDate invoice.date  YYYY-MM-DD }}" />

对于我来说,获得当地日期和以正确格式输入<条码>类型=“日<>/代码>的最短方法是:

var d = new Date(); 
var today = d.getFullYear()+"-"+("0"+(d.getMonth()+1)).slice(-2)+"-"+("0"+d.getDate()).slice(-2);

Or this :

var d = new Date().toLocaleDateString().split( / );
var today = d[2]+"-"+("0"+d[0]).slice(-2)+"-"+("0"+d[1]).slice(-2);

然后确定日期投入价值:

$( #datePicker ).val(today);

问题

$( #datePicker ).val(moment().format( YYYY-MM-DD ));

说明:你需要增加改动。 j 扶养

一种表述的解决办法:

$( #datePicker ).val(
    new Date()
    .toLocaleDateString("en-US", 
    {year:"numeric",month:"2-digit",day:"2-digit"})
   .replace(/(d+).(d+).(d+)/,"$3-$2-$1")
)




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...