Home > other >  How to use Moments to change date format
How to use Moments to change date format

Time:09-14

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 >

    <label for="birth-date" >BIRTH DATE</label>
    <input type="date"   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.

CodePudding user response:

You should use format() method to convert it.

const convertedFormat = moment(dob, 'mm/dd/yyyy').format('dd/mm/yyyy');

OR

const convertedFormat = moment(dob).format('dd/mm/yyyy');

NOTE: I recommend you stop using Moment.js (Why?) and use date-fns instead.

CodePudding user response:

I just changed this :

const date = moment(db, 'yyyy-dd-mm');

to :

const date = moment(db, 'YYYY-DD-MM');
  • Related