默认情况下,输入type="date"显示日期为YYYY-MM-DD。
问题是,是否可能将其格式强制为:DD-MM-YYYY?
默认情况下,输入type="date"显示日期为YYYY-MM-DD。
问题是,是否可能将其格式强制为:DD-MM-YYYY?
当前回答
不是真的没有。
可破解但非常轻薄和可定制的解决方案将是:
隐藏日期输入(CSS可见性:隐藏)(仍然显示日历弹出) 在上面放一个文本输入 在文本输入中单击,使用JS获取日期输入元素并调用.showPicker() 将日期选择器值存储在其他地方 在文本输入中以您想要的自定义格式显示值
下面是一些react代码示例:
<div style={{ width: "100%", position: "relative" }}>
<input type="date" className={`form-control ${props.filters[dateFromAccessor] ? '' : 'bh-hasNoValue'}`} id={`${accessor}-date-from`} placeholder='from'
value={toDate(props.filters[dateFromAccessor])} style={{ marginRight: 0, visibility: "hidden" }}
onChange={e => {
props.setFilters({ ...props.filters, [dateFromAccessor]: inputsValueToNumber(e) })
}} />
<input type="text" className="form-control" readOnly
style={{ position: "absolute", top: 0, left: 0, width: "100%", height: "100%", backgroundColor: "white" }}
value={toDate(props.filters[dateFromAccessor])}
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
(document.getElementById(`${accessor}-date-from`) as any)?.showPicker();
}}></input>
</div>
其他回答
如果你需要一个快速的解决方案,可以试试这个方法,让yyyy-mm-dd变成“dd- september -2016”
1)在您的输入附近创建一个span类(充当标签)
2)每次用户更改日期或需要从数据中加载时更新标签。
工作的webkit浏览器移动和指针事件的IE11+需要jQuery和jQuery日期
$("#date_input").on("change", function () { $(this).css("color", "rgba(0,0,0,0)").siblings(".datepicker_label").css({ "text-align":"center", position: "absolute",left: "10px", top:"14px",width:$(this).width()}).text($(this).val().length == 0 ? "" : ($.datepicker.formatDate($(this).attr("dateformat"), new Date($(this).val())))); }); #date_input{text-indent: -500px;height:25px; width:200px;} <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script> <input id ="date_input" dateformat="d M y" type="date"/> <span class="datepicker_label" style="pointer-events: none;"></span>
我在两年前搜索了这个问题,我的谷歌搜索再次把我带到了这个问题。 不要浪费时间试图用纯JavaScript处理这个问题。我把时间都浪费在日日夜夜的制作上了。没有适合所有浏览器的完整解决方案。所以我建议使用momentJS / jQuery datepicker,或者告诉你的客户端使用默认的日期格式
我调整了米格尔的代码,让它更容易理解 我想和像我一样有问题的人分享。
试试这个简单快捷的方法
$("#datePicker").on("change", function(e) { displayDateFormat($(this), '#datePickerLbl', $(this).val()); }); function displayDateFormat(thisElement, datePickerLblId, dateValue) { $(thisElement).css("color", "rgba(0,0,0,0)") .siblings(`${datePickerLblId}`) .css({ position: "absolute", left: "10px", top: "3px", width: $(this).width() }) .text(dateValue.length == 0 ? "" : (`${getDateFormat(new Date(dateValue))}`)); } function getDateFormat(dateValue) { let d = new Date(dateValue); // this pattern dd/mm/yyyy // you can set pattern you need let dstring = `${("0" + d.getDate()).slice(-2)}/${("0" + (d.getMonth() + 1)).slice(-2)}/${d.getFullYear()}`; return dstring; } .date-selector { position: relative; } .date-selector>input[type=date] { text-indent: -500px; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="date-selector"> <input id="datePicker" class="form-control" type="date" onkeydown="return false" /> <span id="datePickerLbl" style="pointer-events: none;"></span> </div>
自帖子活跃2个月前。所以我想也给大家一些建议
在我的情况下,我从读卡器收到的日期是dd/mm/yyyy格式。
我所做的。 如。
Var d="16/09/2019" //收到卡片的日期 函数filldate () { . getelementbyid (cardexpirydate) value = d.split(“/”).reverse () . join(“-”); } <input type="date" id="cardexpirydate"> <br /><br /> <input type="button" value="填写日期" onclick="filldate();" >
代码的作用:
它根据“/”将我得到的日期分割为dd/mm/yyyy(使用split()),并创建一个数组, 然后它反转数组(使用reverse()),因为日期输入支持反转 我所得到的。 然后,它将(使用join())数组连接到一个字符串,根据 输入字段要求的格式
所有这些都在一行中完成。
我想这会对一些人有所帮助,所以我写了这个。
这是不可能改变web-kit浏览器使用用户的电脑或手机的默认日期格式。 但是如果你可以使用jquery和jquery UI,有一个日期选择器是可设计的,可以显示在任何格式的开发人员想要的。 jquery UI日期选择器的链接是 在这个页面http://jqueryui.com/datepicker/上,您可以找到演示以及代码和文档或文档链接
编辑:-我发现chrome使用的语言设置默认等于系统设置,但用户可以改变他们,但开发人员不能强迫用户这样做,所以你必须使用其他js解决方案,就像我告诉你可以搜索网页查询像javascript日期选择器或jquery日期选择器