admin管理员组文章数量:1356235
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
4 Answers
Reset to default 5you 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();
本文标签: javascriptConverting date format from mmddyyyy to yyyymmdd format after enteredStack Overflow
版权声明:本文标题:javascript - Converting date format from mmddyyyy to yyyy-mm-dd format after entered - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744040553a2580580.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论