0

In my datepicker the date will be inserted in mm/dd/yyyy format. But after I inserted I want it to be sent in yyyy-mm-dd format. I am using JavaScript to do this. But I wasn't able to do that. So what should I do?

Thanks & regards, Chiranthaka

ChiranSJ
  • 195
  • 1
  • 3
  • 20
  • Please show us what you've tried. – George Jul 24 '14 at 11:25
  • This is what I have tried $("#to_date, #from_date").datepicker({maxDate: new Date(),dateFormat:'yyyy-mm-dd'}); But in here the date pass as 20142014-07-10. It duplicates the year. If I change it to yy-mm-dd it pass the date as 2014-07-10. This is what I want. But when I use this within Google Analytics Dashboard it gives me an error regarding using regular expressions. It means the date format is not identified as the correct format. So what do you think about that? – ChiranSJ Jul 24 '14 at 11:34
  • The source code I have used $("#to_date, #from_date").datepicker({maxDate: new Date(),dateFormat:'yyyy-mm-dd'}); – ChiranSJ Jul 24 '14 at 11:41
  • http://stackoverflow.com/questions/1328025/jquery-ui-datepicker-change-date-format Based on this SO question, I used datepicker formatDate to get the format of yyyy-mm-dd: dateObject = $('#datepicker').datepicker('getDate'); var dateString = $.datepicker.formatDate('yy-mm-dd', dateObject); Note that the format specifier only uses yy to get the four-digit date, eg 2017-02-07 When I used yyyy, I got the year twice, as in 20172017-02-07 – adg Feb 07 '17 at 21:40

4 Answers4

5

you could also use regular expressions:

var convertDate = function(usDate) {
  var dateParts = usDate.split(/(\d{1,2})\/(\d{1,2})\/(\d{4})/);
  return dateParts[3] + "-" + dateParts[1] + "-" + dateParts[2];
}

var inDate = "12/06/2013";
var outDate = convertDate(inDate); // 2013-12-06

The expression also works for single digit months and days.

Christof R
  • 863
  • 6
  • 10
1

I did the opposite for my website, but it might help you. I let you modify it in order to fit your requierements. Have fun !

getDate

getMonth

getFullYear

Have fun on W3Schools

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();

if(curr_month < 10)
curr_month = "0"+curr_month;
if(curr_date < 10)
curr_date = "0"+curr_date;
var curr_date_format = curr_date+"/"+curr_month+"/"+curr_year;
ChoiBedal
  • 111
  • 7
  • I am using JQuery date picker to insert dates. So what should I do? – ChiranSJ Jul 24 '14 at 11:35
  • I guess fill up http://jsfiddle.net/ and check out http://api.jqueryui.com/datepicker/#method-getDate . Or I haven't fully understood what you're trying to do. – ChoiBedal Jul 24 '14 at 11:51
  • This is not about entering the current date pal. This is about entering date in mm/dd/yyyy format but it should deliver to the corresponding function as yyyy-mm-yy format. Because Google Analytics supports only supports that format. – ChiranSJ Jul 24 '14 at 12:13
0

Adding more to Christof R's solution (thanks! used it!) to allow for MM-DD-YYYY (- in addition to /) and even MM DD YYYY. Slight change in the regex.

var convertDate = function(usDate) {
  var dateParts = usDate.split(/(\d{1,2})[\/ -](\d{1,2})[\/ -](\d{4})/);
  return dateParts[3] + "-" + dateParts[1] + "-" + dateParts[2];
}

var inDate = "12/06/2013";
var outDate = convertDate(inDate); // 2013-12-06

As Christof R says: This also works for single digit day and month as well.

TheSatinKnight
  • 696
  • 7
  • 16
-1
 // format from M/D/YYYY to YYYYMMDD
 Date.prototype.yyyymmdd = function() {
    var yyyy = this.getFullYear();
    var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based
    var dd = this.getDate() < 10 ? "0" + this.getDate() : this.getDate();
    return "".concat(yyyy).concat(mm).concat(dd);
  };

var siku = new Date();
document.getElementById("day").innerHTML = siku.yyyymmdd();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
maggy
  • 1
  • While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please [EDIT](https://stackoverflow.com/posts/59628321/edit) to provide example code to show what you mean. Alternatively, consider writing this as a comment instead – ρяσѕρєя K Jan 07 '20 at 12:36