[英]input[type=date] format value dynamically generated in php
I have an input with a value dynamically generated 我有一个动态生成值的输入
<input name="etd" type"date" value="<?php echo $row[0]; ?>">
In order to support html5 date input, a date format yyyy-mm-dd
is assigned to that value, and mm/dd/yyyy
format is shown in html5 browsers correctly. 为了支持html5日期输入,日期格式为
yyyy-mm-dd
分配给该值, mm/dd/yyyy
格式正确显示在html5浏览器中。
The problem arises on non html5 browsers where the value assigned is shown as text directly (format yyyy-mm-dd
). 问题出现在非html5浏览器上,其中赋值的值直接显示为文本(格式为
yyyy-mm-dd
)。 My attempt to transform this value to format mm/dd/yyyy
with jQuery is the following: 我试图将此值转换为使用jQuery格式化
mm/dd/yyyy
,如下所示:
<script>
$('input[type="date"]').each({
var now = new Date($(this).attr('value'));
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear()+"-"+(month)+"-"+(day);
$(this).val(today);
});
</script>
but is not working, I'm new to jQuery , so I think, it's something with it. 但是没有用,我是jQuery的新手,所以我想,它就是它的一部分。
UPDATE: 更新:
I'm using this code to put a date picker on non html5 capable browsers, this not solves my problem with the dates retrieved from database, but the if clause somehow can difference html5 browsers. 我正在使用此代码在不支持html5的浏览器上放置日期选择器,这不能解决我从数据库中检索到的日期问题,但if子句在某种程度上可以区分html5浏览器。 I thinking maybe I can use that condition to filter the jQuery code (that code which i'm looking for)
我想也许我可以使用该条件来过滤jQuery代码(我正在寻找的代码)
<script>
$(function() {
if (!Modernizr.inputtypes['date']) {
$('input[type=date]').datepicker({
dateFormat: 'mm/dd/yy'
});
}
});
</script>
Try it like; 试试吧; Here's the Fiddle link .
这是小提琴链接 。
Html: HTML:
<input name="etd" type="date" value="1919-03-12">
Js: JS:
$( document ).ready(function() {
$('input[type=date]').each(function (this){
var datestring = $(this).val();
var dateobj = new Date(datestring);
var formattedstring = dateobj.getUTCDate()+"/"+dateobj.getUTCMonth()+"/"+dateobj.getUTCFullYear();
$(this).val(formattedstring);
});
});
Also please note that when you set the date and month values, for single values there should be a zero in front of it. 另请注意,当您设置日期和月份值时,对于单个值,前面应该有一个零。 For example month 7 should be 07.
例如,第7个月应为07。
Btw, in your code you are missing the = sign in type"date" please correct that too :) 顺便说一句,在您的代码中,您缺少=“登录类型”日期,请更正:)
In JavaScript you can actually use split
on the string. 在JavaScript中,您实际上可以在字符串上使用
split
。 Much easier than passing it through the Date object. 比通过Date对象传递要容易得多。
$('input[type="date"]').each(function(){
var now = $(this).attr('value').split("-");
var today = now[1]+"/"+now[2]+"/"+now[0]; //mm/dd/yyyy
$(this).val(today);
});
split
will hack a string into array pieces using the delimiter. split
将使用分隔符将字符串破解为数组片段。 A dash in this case. 在这种情况下破折号。 The following array is created:
创建以下数组:
['yyyy', 'mm', 'dd'];
Now you can rearrange those array pieces into the desired string. 现在,您可以将这些数组片段重新排列为所需的字符串。
A demo: 演示:
$('input[type="date"]').each(function(){ var now = $(this).attr('value').split("-"); var today = now[1]+"/"+now[2]+"/"+now[0]; //mm/dd/yyyy $(this).val(today); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="date" value="2015-08-09" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.