简体   繁体   English

无法更改日期选择器中的日期格式

[英]Can't change date format in datepicker

I have a datepicker where it disables all the dates that is stored within the database, and it works just fine. 我有一个datepicker,它禁用了数据库中存储的所有日期,并且工作正常。 Although i have a problem, the input format that is currently on is mm-dd-yy and my date format in my database is yy-mm-dd which causes problem from my insertion. 尽管我有问题,但是当前使用的输入格式是mm-dd-yy,而数据库中的日期格式是yy-mm-dd,这会导致我的插入出现问题。 I tried adding dateFormat: yy-mm-dd and it does not work, i tried placing it outside and inside the function and all it does is making my datepicker not functional. 我尝试添加dateFormat:yy-mm-dd,但它不起作用,我尝试将其放置在函数的内部和外部,它所做的只是使我的datepicker无法运行。 I really need some assistance, thank you! 我真的需要一些帮助,谢谢! I'm sorry if its a silly question but I'm still new to the jquery stuff. 我很抱歉,如果这是一个愚蠢的问题,但我还是jquery的新手。 I tried searching everywhere but since i have a beforeShowDay:function(date) function, i cant seem to fix my problem... 我尝试到处搜索,但是由于我有一个beforeShowDay:function(date)函数,所以我似乎无法解决我的问题...

Yes i could just change the format of my date column in my database but i have my other guys working on an admin panel application for the website we're connected to one database, but i can't contact them right now. 是的,我只可以更改数据库中日期列的格式,但是我让其他人正在为我们连接到一个数据库的网站开发管理面板应用程序,但现在无法联系他们。 Thank you! 谢谢!

PHP PHP

<?php  
$query = " SELECT  * FROM event_table ";
$result = mysqli_query($conn,$query);
$sentToList = array();
while($row = mysqli_fetch_assoc($result)) { 

  $sentToList[] = $row['event_date'];

   }
$json = json_encode($sentToList);

?>

SCRIPT 脚本

<script >

    var dateToday = new Date(); 
    $(function() {

    var array = <?php print_r($json) ?>

    $('#mydate').datepicker({

        beforeShowDay: function(date){


            var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
            return [ array.indexOf(string) == -1 ]

        }

    });
});
</script>

I assume you are using the jQueryUI datepicker . 我假设你正在使用jQueryUI的日期选择器

Now if you would check their API list, you will see how can you format your date. 现在,如果您要查看他们的API列表,您将看到如何格式化日期。

Let us know if you have any further problems. 让我们知道您是否还有其他问题。

Shouldn't you define the format like this? 您不应该这样定义格式吗?

$('#mydate').datepicker({ dateFormat: 'yy-mm-dd' });

If this is what you've tried already what do you mean with "datepicker not functional", do you get any errors? 如果您已经尝试过此操作,那么“ datepicker无法运行”是什么意思,您会遇到任何错误吗?

A number of things to help with here: 这里有很多帮助的事情:


Converting your PHP array to JSON 将您的PHP数组转换为JSON

When you get your JSON encoded 'string' from PHP you then need to parse it as a JSON object using JSON.parse(*yourJSONString*); 当您从PHP获取JSON编码的'string' ,您需要使用JSON.parse(*yourJSONString*);将其解析为JSON对象JSON.parse(*yourJSONString*);


Date formatting issue 日期格式问题

Date format without the leading '20' for year requires you to use only one y in your format string. 如果日期格式不带year的前导'20' ,则您只需在格式字符串中使用一个y

dateFormat: 'y-mm-dd'

Adding the red X's style over disabled dates 在禁用日期上添加红色X的样式

The red X's over disabled dates also requires a bit of CSS. 红色X的禁用日期过长也需要一些CSS。

.ui-datepicker-calendar>tbody>tr>td.ui-datepicker-unselectable>span.ui-state-default:before{
  bottom: 0;
  content: "X";
  height: 4px;
  left: 7px;
  margin: auto;
  position: relative;
  right: 0;
  top: 0;
  width: 4px;
  color: #FF0000;
}

Optional extra JQuery UI plugin 可选的额外JQuery UI插件

You can even use the tooltip plugin to make selectable dates more obvious 您甚至可以使用工具提示插件使可选日期更加明显


Example

 $( function() { //build up your array //this is a mock of var arrayFromPHP = <?php echo json_encode($json); ?>; var phpArrayString = '["17-09-30","17-09-29", "17-09-28", "17-09-15", "17-09-09"]'; //parse your json here var array = JSON.parse(phpArrayString); //include dateFormat AND beforeShowDay options $( "#mydate" ).datepicker({ dateFormat: 'y-mm-dd', beforeShowDay: function(date) { var string = jQuery.datepicker.formatDate('y-mm-dd', date); //You can even use tooltips to make dates you can select more obvious return [ array.indexOf(string) == -1, 'highlight', 'You may select this date' ]; } }); //Optionally - call the tooltip plugin on dates that can be selected $('#mydate .highlight a').tooltip(); }); 
 /* Add this to your stylesheet to include the red X's */ .ui-datepicker-calendar>tbody>tr>td.ui-datepicker-unselectable>span.ui-state-default:before{ bottom: 0; content: "X"; height: 4px; left: 7px; margin: auto; position: relative; right: 0; top: 0; width: 4px; color: #FF0000; } /* Simple utility classes for example styling */ .bordered{ border: 1px solid black; } .padded{ padding: 5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/> <div class="bordered"> <div class="padded"> <label for="mydate"> Date </label> <br /> <input type="text" id="mydate" size="30" > </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM