MySQL node.JS 中的日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11187961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:56:46  来源:igfitidea点击:

date format in node.JS

mysqlnode.js

提问by nithin Kumar

I am using mysql database,in that i have a field by name request_date. The type of the field is time stamp and the data stored in this field has the format 2012-05-16 14:59:18. But when I retrieve the same data by Node.JS, the data is displayed in the browser as

我正在使用 mysql 数据库,因为我有一个名称字段request_date。该字段的类型为时间戳,该字段中存储的数据格式为2012-05-16 14:59:18. 但是当我通过 Node.JS 检索相同的数据时,数据在浏览器中显示为

Fri Jun 08 2012 15:16:50 GMT+0530 (India Standard Time)

Why this format change is happening?

为什么会发生这种格式更改?

I have written this query:

我写了这个查询:

SELECT
biz_registration.reqid,biz_registration.request_date,biz_registration.req_status,biz_registration.annual_cost,biz_registration.rid,biz_contact.first_name,biz_contact.last_name
FROM biz_registration
INNER JOIN biz_contact ON biz_registration.reqid=biz_contact.reqid
ORDER BY biz_registration.request_date
DESC limit '+start+','+mlimit+''

the html code i am using is,

我正在使用的 html 代码是,

options +='<div class="column" style="background-color: #E1E1E1;width:         100px;">'+result.reqid+'</div>';
options +='<div class="column" style="background-color: #E1E1E1;width: 160px;">'+result.request_date+'</div>';

回答by Balkana

I was having the same problem, this fixed my problem:

我遇到了同样的问题,这解决了我的问题:

You need to 'force' your mysql connection to format it immediately, add this into your config(connection details):

您需要“强制”您的 mysql 连接立即对其进行格式化,将其添加到您的配置中(连接详细信息):

host: 'localhost',
user: 'root'
// ....
dateStrings: 'date'

回答by RANDRIAMILASOA Stanislas

This config resolve mine, according to node-mysql doc

根据node-mysql doc,此配置解决了我的问题

  host: 'localhost',
  user: 'root',
  password: '',
  dateStrings:true,
  database: "mydb"

回答by nithin Kumar

i got this working by requiring the library or module called date format. first we have to install date format package using

我通过要求称为日期格式的库或模块来实现此目的。首先我们必须使用安装日期格式包

npm install dateformat

npm 安装日期格式

then u can require it in ur coding. then u can create the object of retrieved data as

然后你可以在你的编码中要求它。然后你可以创建检索数据的对象

var day=dateFormat(result.request_date, "yyyy-mm-dd h:MM:ss");

var day=dateFormat(result.request_date, "yyyy-mm-dd h:MM:ss");

and print it.

并打印出来。

回答by Martin Faartoft

What you see, is simply the default javascript date formatting. If you want it formatted differently, you can use the methods:

您所看到的只是默认的 javascript 日期格式。如果您希望它的格式不同,可以使用以下方法:

getDate(): Returns the date
getMonth(): Returns the month
getFullYear(): Returns the year

on the date object, and combine the results with / or - or : to get the format you want.

在日期对象上,并将结果与​​ / 或 - 或 : 组合以获得所需的格式。

Have a look at: http://www.elated.com/articles/working-with-dates/for more details

查看:http: //www.elated.com/articles/working-with-dates/了解更多详情

回答by M. Hamza Rajput

Here is the solution:

这是解决方案

var datetme = new Date().toLocaleString();

OR

或者

const DATE_FORMATER = require( 'dateformat' );
var datetme = DATE_FORMATER( new Date(), "yyyy-mm-dd HH:MM:ss" );