-9

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,

fo example from "1993-12-12T00:00:00.000Z" to "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 :

ImenElAbed
  • 11
  • 6
  • 3
    What do you mean by "convert"? Why not simply check for the `T` and split on that? Are there any special cases? – Nico Haase Apr 01 '19 at 12:37
  • Is your input just a string? A `Date` object? Something else? – David Apr 01 '19 at 12:38
  • i have an input in a form with a type=date , – ImenElAbed Apr 01 '19 at 12:38
  • `"1993-12-12T00:00:00.000Z".slice(0,10)` – Code Maniac Apr 01 '19 at 12:39
  • it shows 1993/12/2012 but in the database (mongodb) it became 1993-12-12T00:00:00.000Z – ImenElAbed Apr 01 '19 at 12:40
  • 1
    @ImenElAbed: Your question makes no mention of a database nor shows any code which writes to or reads from a database. Please explain the actual problem. – David Apr 01 '19 at 12:42
  • 1
    Possible duplicate of [Format JavaScript Date to yyyy-mm-dd](https://stackoverflow.com/questions/23593052/format-javascript-date-to-yyyy-mm-dd) and [How to get current formatted date dd/mm/yyyy in Javascript and append it to an input](https://stackoverflow.com/questions/12409299) and [How do I get a date in YYYY-MM-DD format?](https://stackoverflow.com/questions/32192922) and [Get String in YYYYMMDD format from JS date object?](https://stackoverflow.com/questions/3066586) and [How to get date in format yyyy-mm-dd in javascript?](https://stackoverflow.com/questions/46297041) – adiga Apr 01 '19 at 12:43
  • 1
    [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/a/261593/3082296) – adiga Apr 01 '19 at 12:43
  • Possible duplicate of [How to parse ISO 8601 into date and time format using Moment js in Javascript?](https://stackoverflow.com/questions/39735724/how-to-parse-iso-8601-into-date-and-time-format-using-moment-js-in-javascript) – Aditya Dhanraj Apr 01 '19 at 13:54

3 Answers3

1

you can use Moment js for any type of Date conversation.

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"

`


KARNAV PARGI
  • 139
  • 1
  • 1
  • 9
  • how to generalize this in a function because i want to save any input in format (DD/MM/YYYY) , thank you – ImenElAbed Apr 01 '19 at 12:45
  • 1
    `convertDate=(date)=>{ let cvDate = moment(date).format('DD/MM/YYYY'); return cvDate}` or `convertDate=(date, format)=>{ let cvDate = moment(date).format(format); return cvDate}` – KARNAV PARGI Apr 01 '19 at 12:54
1

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)
0

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);