简体   繁体   English

将 HTML 日期转换为 Mysql 格式

[英]Convert HTML Date to Mysql Format

I am using a bootstrapdatetimepicker as well as moment.js我正在使用 bootstrapdatetimepicker 以及 moment.js

When i call the following当我打电话给以下

var starteventtime= ($('#startdatetime').data('DateTimePicker').date());

I get a date similar to我得到一个类似于

Wed Mar 27 2019 00:00:00 GMT+0000

I also have the option for the user to save their own format of the date.我还可以让用户选择保存他们自己的日期格式。 So any code will have to take this into consideration.所以任何代码都必须考虑到这一点。

<?php
   $datetimeformat = 'DD/MM/YYYY HH:mm';
 ?>
    $(function() {
             $('#startdatetime').datetimepicker({
             format: '<?php echo $datetimeformat; ?>'
    });

How do i parse this on the client side with javascript to get Mysql formatted date so when the data is serialized it will be in a format ready for insertion into the database like below我如何在客户端使用 javascript 解析它以获取 Mysql 格式的日期,以便在数据被序列化时它将采用准备插入数据库的格式,如下所示

2019-03-27 00:00:00

I am quite certain whatever the date time picker returns is a Date object and not a string (if it isn't, use another picker, because this one is useless).我很确定无论日期时间选择器返回的是Date对象而不是字符串(如果不是,请使用另一个选择器,因为这个没用)。

When you have the Date object, you can format that however you want to insert it into your database.当您拥有Date对象时,您可以格式化它,但您想将其插入到数据库中。 For example with the answer given here: How to format a JavaScript date例如这里给出的答案: How to format a JavaScript date

You should look into converting your dates into a standard Unix timestamp.您应该考虑将日期转换为标准的 Unix 时间戳。 Its super easy in every environment you're working in (Mysql, php, javascript).它在您工作的每个环境(Mysql、php、javascript)中都非常简单。 For example, in javascript Date.now() returns the current time as a Unix timestamp.例如,在 javascript Date.now()返回当前时间作为 Unix 时间戳。 Look up how to do that for the other places you're working with time.看看如何为你正在工作的其他地方做这件事。 With that as a standard, you can just pass around the timestamps for your dates without having to worry about formatting until you're ready to display them.以此为标准,您可以只传递日期的时间戳,而不必担心格式化,直到您准备好显示它们。

Thanks for the advice , i used谢谢你的建议,我用过

moment($('#startdatetime').data('DateTimePicker').date()).format("YYYY/MM/DD HH:mm"));

to convert it to an object that was in mysql format.将其转换为 mysql 格式的对象。

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

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