I’m working on a project, but I’m facing an issue,i want to change datepicker format to from mm-dd-yyyy to yyyy-dd-mm the fuction dont work maybe i’m missing somthing, so how to use Moments.js to change date format from mm-dd-yyyy to yyyy-dd-mm, any help will be appreciated.
here is my code
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
<label for="birth-date" >BIRTH DATE</label>
<input type="date" class="datepicker" name="birth-date" id="birth-date" >
// Datepicker
$('.datepicker').datepicker({
format: 'yyyy-dd-mm',
});
//main function work’s but dateformat is mm/dd/yyyy
const getdate= (db) => {
return ~~((new Date()-new Date(db))/(31556952000));
};
//what i have tried but didn’t work
const getdate = (db) => {
const date = moment(db, 'yyyy-dd-mm');
return moment().diff(date, 'years');
};
Best regards.
2
Answers
I just changed this :
to :
You should use
format()
method to convert it.OR
NOTE: I recommend you stop using
Moment.js
(Why?) and use date-fns instead.