-3

I have a date field on which I am calling datepicker in the following format :

$(function() {
      $('.dob').datepicker({
            dateFormat: 'dd/MM/yyyy',

      });
});

When the user selects the date, I am getting, say,

02 March 2002

I want to display this date in above format only but save as dd/mm/yyyy i.e .

02/03/2002

How to convert my String dd/MM/yyyy to String dd/mm/yyyy? Have tried with simpledateformat, also by providing locale arguments, none of these worked.

Yogi
  • 6,241
  • 3
  • 24
  • 30
Ananya
  • 23
  • 3
  • 4
    Please be aware that JavaScript is not Java. What are you looking for? You've put Java in the title, given JavaScript code, and tagged both. – evolutionxbox Mar 19 '23 at 13:07
  • No, I want to display month name which is why I have taken MM but want to convert it to 03 before saving in db. – Ananya Mar 19 '23 at 13:18
  • Please take care with adding tags. You have a tag there that is unrelated to javascript, and there are no tags that indicate which library and widget you are using. – trincot Mar 19 '23 at 13:37
  • `MM` = `03`, `MMM` = `Mar`, `mm` = minutes - in *most* sensible date/time formatting conventions. – freedomn-m Mar 19 '23 at 15:57

1 Answers1

2

If you want to show the date as 02 March 2002, you should change the dateFormat to the following:

dateFormat: 'dd MM yy'

All the formatting options for the jQuery DatePicker can be found here.

It is not recommended to save the date as a string but rather the corresponding date object supported by the database. If you still wish to get the date as dd/mm/yyyy, it could be done like this:

let dateAsString;
$(function() {
  $('.dob').datepicker({
        dateFormat: 'dd MM yy',
        onSelect: function() {
            let dateObj = $(this).datepicker('getDate') // Retrieves the selected date
            let yy = dateObj.getFullYear()
            let mm = dateObj.getMonth() + 1 // Month starts at 0
            if (mm < 10) mm = '0' + mm
            let dd = dateObj.getDate()
            if (dd < 10) dd = '0' + dd

            dateAsString = dd + '/' + mm '/' + yy
        }
  });
});

dateAsString will be the string you are looking for.

The formatting idea is from this answer.

Rayantjhu
  • 31
  • 3
  • 1
    That would result in user input display as '02/03/2002' but I want to show it as 02 March 2002 and then convert March into integer before saving in db. – Ananya Mar 19 '23 at 13:20
  • I find it quite disturbing that you updated your answer with the information I had put in my answer, even using the same locale argument. If you decide to reuse information from another answer, then the least you can do is add an attribution. – trincot Mar 19 '23 at 14:39
  • @trincot I didn't mean to use anything from your answer, I'm pretty sure that at around the time I updated my answer after seeing the comment, that you posted yours. And yes I did copy the locale argument from you after seeing your answer, as I thought it is a better alternative than what I had before. I'll update my answer with what I previously used, as I don't think it's fair that I use something from your answer. My bad. – Rayantjhu Mar 19 '23 at 14:50
  • Well my answer which came 10 minutes before your update is of no more use now. I have deleted it. – trincot Mar 19 '23 at 14:53