dtjzpg5313 2015-08-09 13:50
浏览 39
已采纳

输入[type = date]格式值在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.

The problem arises on non html5 browsers where the value assigned is shown as text directly (format yyyy-mm-dd). My attempt to transform this value to format mm/dd/yyyy with jQuery is the following:

<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.

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. I thinking maybe I can use that condition to filter the jQuery code (that code which i'm looking for)

<script>
    $(function() {
        if (!Modernizr.inputtypes['date']) {
            $('input[type=date]').datepicker({
                dateFormat: 'mm/dd/yy'
            });
        }
    });
</script>
  • 写回答

2条回答 默认 最新

  • douzhangli9563 2015-08-09 14:33
    关注

    Try it like; Here's the Fiddle link.

    Html:

    <input name="etd" type="date" value="1919-03-12">
    

    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.

    Btw, in your code you are missing the = sign in type"date" please correct that too :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器