简体   繁体   English

如何在没有小时的情况下转换日期格式?

[英]How to convert date format without hours?

i have a html form where i will enter a date after that this date will be saved in my database (mongodb) , the problem is that the date in data becames with hours,我有一个 html 表单,我将在其中输入一个日期,然后将该日期保存在我的数据库(mongodb)中,问题是数据中的日期变成了小时,

fo example from "1993-12-12T00:00:00.000Z" to "1993-12-12"例如从“1993-12-12T00:00:00.000Z”到“1993-12-12”
please help me in this请帮助我

 <form action="/adddoctor" method="POST" class="form-horizontal" role="form" > <input type="date" class="form-control" min="15/12/1980" max="15/12/1993" name="date_birth" id="date_birth" placeholder="Date de naissannce *" value="" required /> </form>
 app.post('/adddoctor', function (req,res) { var usermed = new users ( { date_birth:req.body.date_birth, } ); usermed.save( function (err) { if (err) { console.log(err) res.send("error") } res.sendFile(__dirname + "/public/adddoctor.html"); }); })

This is my nodejs and html code :这是我的 nodejs 和 html 代码:

you can use Moment js for any type of Date conversation.您可以将 Moment js 用于任何类型的 Date 对话。

moment("1993-12-12T00:00:00.000Z").format('DD/MM/YYYY') = "12/12/1993"
or
moment("1993-12-12T00:00:00.000Z").format('YYYY-MM-DD') = "1993-12-12"

`


You could try something like this:你可以尝试这样的事情:

 var date = new Date('1993-12-12T00:00:00.000Z'); var newdate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() alert(newdate)

You can do it through simple string manipulation.您可以通过简单的字符串操作来实现。

 var str = '9999-12-31T00:00:00Z'; var parts = str.slice(0, -1).split('T'); var dateComponent = parts[0]; var timeComponent = parts[1]; console.log(dateComponent); console.log(timeComponent);

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

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