使用javascript从mysql数据库显示类似30-04-2020的日期而不是2020-04-30 [英] Show date like 30-04-2020 instead of 2020-04-30 from mysql database using javascript
问题描述
我正在构建用于个人业务目的的小型Web应用程序.我远不止四处寻找如何对Web应用程序进行编程.因此,我可以添加保留并对其进行编辑,也可以将其删除.最近几天,我一直在寻找如何将日期保存到Mysql数据库中以及如何在Web应用程序中显示它.到目前为止,它的工作正常,但没有以正确的格式显示.如果您看到附件日期"示例,我想要的日期是"30-04-2020"而不是"2020-04-30".由于某种原因,我无法对其进行配置.有人可以帮我吗.我会放一些代码.下面的代码
I'am building a small webapplication for personal business purpose. I have come far from looking around and looking how to program a webapplication. So I can added reservations and edit them and also delete them. The last days I have been looking how to save a date into Mysql database and how to show it in the webapplication. For so far its working but its not showing in the right format. If you see the attachment Date sample I want the date like this "30-04-2020" and not like "2020-04-30". For some reason I just cant configure it. Can someone help me with that. I will put some code. The code below
数据库连接;
const db = mysql.createConnection ({
host: 'localhost',
user: 'root',
password: '',
database: '',
dateStrings: true,
});
我的插入查询
let query = "INSERT INTO `players` (achternaam, telefoonnummer, adres, email, typetaart, aantalpersonen, smaak, vulling, opmerking, prijs, reedsVoldaan, nogTeVoldoen, date, image, user_name) VALUES ('" +
achternaam + "', '" + telefoonnummer + "', '" + adres + "', '" + email + "', '" + typetaart + "', '" + aantalpersonen + "', '" + smaak + "','" + vulling + "', '" + opmerking + "', '" + prijs + "', '" + reedsVoldaan + "', '" + nogTeVoldoen + "',STR_TO_DATE ('" + date + "', '%d-%m-%Y'), '" + image_name + "', '" + username + "')";
主页
exports.homepageMTaartenAdmin = function(req, res, next){
var user = req.session.user,
userId = req.session.userId;
console.log('ddd='+userId);
if(userId == null){
res.redirect("login");
return;
}
var sql="SELECT * FROM `players` ORDER BY id ASC"; // query database to get all the players
db.query(sql, function(err, result){
res.render('homepageMTaartenAdmin', {players:result});
});
}
日期样本
如果您需要任何其他信息,请告诉我.我感谢您的帮助.
If you need any further information please tell me. I appreciate your help.
亲切的问候
威廉·阿肖蒂
推荐答案
In MySQL, you can use date function date_format()
to display your date in the desired string format:
SELECT
achternaam,
telefoonnummer,
adres,
email,
typetaart,
aantalpersonen,
smaak,
vulling,
opmerking,
prijs,
reedsVoldaan,
nogTeVoldoen,
DATE_FORMAT(date, '%d-%m-%Y') date, --> here
image,
user_name
FROM `players`
ORDER BY id ASC
如果要避免枚举所有列,则可以在结果集中添加具有不同标识符的新列,例如:
If you want to avoid enumerating all the columns, you can add a new column in the result set with a different identifier, like:
SELECT p.*, DATE_FORMAT(date, '%d-%m-%Y') formatted_date
FROM `players` p
ORDER BY id ASC
这篇关于使用javascript从mysql数据库显示类似30-04-2020的日期而不是2020-04-30的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!