日期格式化(包括日期对象和字符串)

来源:互联网 发布:设备管理相关软件 编辑:程序博客网 时间:2024/06/11 17:38
dateObj = {reg: /^[0-9]{4}[-|/](0?[1-9]|1[0-2])[-|/]((0?[1-9])|((1|2)[0-9])|30|31)$/,formatDate: function(dates, symbol, cpt) {//dates:日期,可以是日期对象(new Date()),可以是字符串"2017-05-20"//symbol:年月日之间的分割符//cpt:月和日小于9时是否自动补零var that = this;if (symbol === undefined) {symbol = "/"}if (cpt === undefined) {cpt = true}if (dates instanceof Date) {var year = dates.getFullYear(),month = dates.getMonth() + 1,day = dates.getDate() if (cpt) {month = month < 10 ? "0" + month : month;day = day < 10 ? "0" + day : day}return year + symbol + month + symbol + day} else if (typeof dates == "string" && that.reg.test(dates)) {var oldsymbol = dates[4];var arr = dates.split(oldsymbol);var year = Number(arr[0]),month = Number(arr[1]),day = Number(arr[2]);if (cpt) {month = month < 10 ? "0" + month : month;day = day < 10 ? "0" + day : day}return year + symbol + month + symbol + day} else {return ""}},splitDate: function(dates) {var that = this;if (typeof dates == "string" && that.reg.test(dates)) {var oldsymbol = dates[4];var arr = dates.split(oldsymbol);return arr} else {console.warn("the string is not a dateString");return []}}}

0 0
原创粉丝点击