最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting date format from mmddyyyy to yyyy-mm-dd format after entered - Stack Overflow

programmeradmin1浏览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

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

Share Improve this question asked Jul 24, 2014 at 11:24 ChiranSJChiranSJ 1451 gold badge3 silver badges21 bronze badges 4
  • Please show us what you've tried. – George Commented Jul 24, 2014 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 Commented Jul 24, 2014 at 11:34
  • The source code I have used <code> $("#to_date, #from_date").datepicker({maxDate: new Date(),dateFormat:'yyyy-mm-dd'}); </code> – ChiranSJ Commented Jul 24, 2014 at 11:41
  • stackoverflow./questions/1328025/… 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 Commented Feb 7, 2017 at 21:40
Add a ment  | 

4 Answers 4

Reset to default 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.

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;

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.

 // 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();
发布评论

评论列表(0)

  1. 暂无评论