简体   繁体   English

在插入MySQL之前,Bootstrap Datepicker将格式从dd.mm.yyyy转换为yyyy-mm-dd

[英]Bootstrap Datepicker convert format from dd.mm.yyyy to yyyy-mm-dd before inserting to MySQL

Is there a fairly simple way to convert the format of Bootstrap Datepicker before inserting the data to database? 在将数据插入数据库之前,是否有一种相当简单的方法来转换Bootstrap Datepicker的格式?

My page had the HTML5 date input until I realized it was only working in Chrome. 我的页面有HTML5日期输入,直到我意识到它只在Chrome中工作。

The thing is I need to display the data on the webpage in a norwegian/european format: dd.mm.yyyy and then insert the value as a "default" date (yyyy-mm-dd) to MySQL. 问题是我需要以挪威/欧洲格式在网页上显示数据:dd.mm.yyyy然后将值作为“默认”日期(yyyy-mm-dd)插入MySQL。 Column in database is Date type. 数据库中的列是日期类型。

I'm not good at javascript, so I'm hoping there's a simple addon to the script that fixes this. 我不擅长使用javascript,所以我希望脚本上有一个简单的插件来解决这个问题。

<!-- Datepicker JS -->
<script src="datepicker/js/bootstrap-datepicker.js"></script>
<script src="datepicker/locales/bootstrap-datepicker.no.min.js" charset="UTF-8"></script>

<script type="text/javascript">
  $(function () {
  $('#date').datepicker({
  locale: 'no'
  });
});
</script>

<div class="col-md-2 form-group">
        <label for="date">Date</label>
        <input type="text" class="form-control input-sm" name="date" id="date" data-provide="datepicker">
</div>

mysql database needs date format in Ymd format so in case to insert this date format in the database you will need to change the date format that comes from datepicker. mysql数据库需要Ymd格式的日期格式,因此如果要在数据库中插入此日期格式,您需要更改来自datepicker的日期格式。

user this code to convert 用户此代码转换

date('Y-m-D',strtotime('YOUR DATEPICKER DATE'));

Use this Pal, this will work I think: 使用这个Pal,我认为这将有效:

$('#date').datepicker({
    format: 'YYYY-MM-DD HH:mm:ss'
});

or this will also work: 或者这也有效:

$('#date').datepicker({
dateFormat: 'DD, MM, d, yy'
});

User following code to get yyyy-mm-dd format. 用户使用以下代码获取yyyy-mm-dd格式。

<script type="text/javascript">
$(function () {
  $('#date').datepicker({
  locale: 'no',
  format: 'yyyy-mm-dd'
  });
});
</script>

Hi try this dirst you have to add this plugin in your script files then you try this code same as like which i gave. 嗨试试这个dirst你必须在你的脚本文件中添加这个插件,然后你尝试这个代码,就像我给的那样。 and you can change the format. 你可以改变格式。

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
  <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>

  <script type="text/javascript">
    $(document).ready(function () {
    $("#Date").datepicker({
        //maxDate: "+0D",
        //changeMonth: true,
        //changeYear: true,
        //yearRange: "-100:+0",
        //minDate: '-0d',
        dateFormat: "M/dd/yy",
        });
     });

   </script>

jquery中,我们可以将日期转换为任何格式,然后使用此自定义格式通过ajax发送到服务器端,这样:

$("#date").data('datepicker').getFormattedDate('yyyy-mm-dd');

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

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