日期格式化(包括日期对象和字符串)
2017-05-12 18:11
253 查看
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 [] } } }
相关文章推荐
- Java中日期格式化成字符串和字符串解析成日期对象工具类
- json对象的用法包括日期格式化
- JavaScript中扩展字符串与Date对象之间的转换,支持大多数日期格式的字符串(包括中文)
- json对象的用法包括日期格式化
- JavaScript中扩展字符串与Date对象之间的转换,支持大多数日期格式的字符串(包括中文)
- js 日期格式化 及 字符串转日期对象
- Java 把日期时间字符串 格式化为Date对象
- JavaScript中扩展字符串与Date对象之间的转换,支持大多数日期格式的字符串(包括中文)
- 对象转JSONArray,JSONObject[包括对象中日期格式化,属性过滤]
- js中日期格式字符串转为日期对象和日期进行格式化
- javascript日期对象格式化为字符串的实现方法
- 格式化--包括字符串, 数字, 日期, 枚举等类型数据的格式化
- 在C#中SendMessage和PostMessage的参数传递(包括对象和字符串)
- javascript日期格式化(转字符串)
- 如何将一个java对象(包括java对象数组、集合)转换成一个json字符串
- 将日期字符串格式化为年月日格式如2011-12-14 、16日、18日-19日、21日、23日、25日-26日、28日
- Java字符串,日期的格式化
- javascript中根据表达式实现字符串格式化为日期——升级版
- javaScript日期对象格式化