skip to Main Content

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


  1. Chosen as BEST ANSWER

    I just changed this :

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

    to :

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

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search