微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript中对Date类型的常用操作小结

javascript中对Date类型的常用操作小结

9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100)); 138. 139. str=str.replace(/MM/,this.getMonth()>=9?(parseInt(this.getMonth())+1).toString():'0' + (parseInt(this.getMonth())+1)); 140. str=str.replace(/M/g,(parseInt(this.getMonth())+1)); 141. 142. str=str.replace(/w|W/g,Week[this.getDay()]); 143. 144. str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate()); 145. str=str.replace(/d|D/g,this.getDate()); 146. 147. str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours()); 148. str=str.replace(/h|H/g,this.getHours()); 149. str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes()); 150. str=str.replace(/m/g,this.getMinutes()); 151. 152. str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds()); 153. str=str.replace(/s|S/g,this.getSeconds()); 154. 155. str=str.replace(/iii/g,this.getMilliseconds()<10?'00'+this.getMilliseconds():(this.getMilliseconds()<100?'0'+this.getMilliseconds():this.getMilliseconds())); 156. 157. return str; 158.} 159. 160. 161./** 162. * 字符串转成日期类型: 163. * dateStr:必选,日期字符串,如果无法解析成日期类型,返回null 164. * 格式: 165. * (1)yyyy/MM/dd:IE和FF通用 166. * (2)MM/dd/yyyy:IE和FF通用 167. * (3)MM-dd-yyyy:仅IE 168. * (4)yyyy-MM-dd:非IE,且时钟被解析在8点整 169. */ 170.Date.stringToDate = function(dateStr) 171.{ 172. if(!dateStr){ 173. alert("字符串无法解析为日期"); 174. return null; 175. }else{ 176. if(Date.isValiDate(dateStr,"yyyy/MM/dd")||Date.isValiDate(dateStr,"MM/dd/yyyy")){ 177. return new Date(Date.parse(dateStr)); 178. }else{ 179. if((!-[1,])){//IE 180. if(Date.isValiDate(dateStr,"MM-dd-yyyy")){ 181. return new Date(Date.parse(dateStr)); 182. }else{ 183. alert("字符串无法解析为日期"); 184. return null; 185. } 186. }else{//非IE 187. if(Date.isValiDate(dateStr,"yyyy-MM-dd")){ 188. return new Date(Date.parse(dateStr)); 189. }else{ 190. alert("字符串无法解析为日期"); 191. return null; 192. } 193. } 194. } 195. } 196. return null; 197.} 198. 199. 200./** 201. * 计算两个日期的天数差: 202. * dateOne:必选,必须是Data类型的实例 203. * dateTwo:必选,必须是Data类型的实例 204. */ 205.Date.daysBetween = function(dateOne,dateTwo) 206.{ 207. if((dateOne instanceof Date)==false||(dateTwo instanceof Date)==false){ 208. return 0; 209. }else{ 210. return Math.abs(Math.floor((dateOne.getTime()-dateTwo.getTime())/1000/60/60/24)); 211. } 212.} 213. 214. 215./** 216. * 日期计算:支持负数,即可加可减,返回计算后的日期 217. * num:必选,必须是数字,且正数是时期加,负数是日期减 218. * field:可选,标识是在哪个字段上进行相加或相减,字段见如下的约定。无此参数时,认为d 219. * 约定如下格式: 220. * (1)Y/y 年 221. * (2)M 月 222. * (3)W/w 周 223. * (4)D/d 日 224. * (5)H/h 时 225. * (6)m 分 226. * (7)S/s 秒 227. * (8)Q/q 季 228. */ 229.Date.prototype.dateAdd = function(num,field) 230.{ 231. if((!num)||isNaN(num)||parseInt(num)==0){ 232. return this; 233. } 234. if(!field){ 235. field = "d"; 236. } 237. switch(field){ 238. case 'Y': 239. case 'y':return new Date((this.getFullYear()+num),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());break; 240. case 'Q': 241. case 'q':return new Date(this.getFullYear(),(this.getMonth()+num*3),this.getSeconds());break; 242. case 'M':return new Date(this.getFullYear(),this.getMonth()+num,this.getSeconds());break; 243. case 'W': 244. case 'w':return new Date(Date.parse(this) + ((86400000 * 7) * num));break; 245. case 'D': 246. case 'd':return new Date(Date.parse(this) + (86400000 * num));break; 247. case 'H': 248. case 'h':return new Date(Date.parse(this) + (3600000 * num));break; 249. case 'm':return new Date(Date.parse(this) + (60000 * num));break; 250. case 'S': 251. case 's':return new Date(Date.parse(this) + (1000 * num));break; 252. default: return this; 253. } 254. return this; 255.} 256. 257. 258./** 259. * 比较日期差:比较两个时期相同的字段,返回相差值 260. * dtEnd:必选,必须是Data类型的实例 261. * field:可选,标识是在哪个字段上进行比较,字段见如下的约定。无此参数时,认为d 262. * 约定如下格式: 263. * (1)Y/y 年 264. * (2)M 月 265. * (3)W/w 周 266. * (4)D/d 日 267. * (5)H/h 时 268. * (6)m 分 269. * (7)S/s 秒 270. */ 271.Date.prototype.dateDiff = function(dtEnd,field) 272.{ 273. var dtStart = this; 274. if((dtEnd instanceof Date)==false){ 275. return 0; 276. }else{ 277. if(!field){ 278. field = "d"; 279. } 280. switch(field){ 281. case 'Y': 282. case 'y':return dtEnd.getFullYear() - dtStart.getFullYear();break; 283. case 'M':return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);break; 284. case 'W': 285. case 'w':return parseInt((dtEnd - dtStart) / (86400000 * 7));break; 286. case 'D': 287. case 'd':return parseInt((dtEnd - dtStart) / 86400000);break; 288. case 'H': 289. case 'h':return parseInt((dtEnd - dtStart) / 3600000);break; 290. case 'm':return parseInt((dtEnd - dtStart) / 60000);break; 291. case 'S': 292. case 's':return parseInt((dtEnd - dtStart) / 1000);break; 293. default: return 0; 294. } 295. return 0; 296. } 297.} 298. 299. 300./** 301. * 把日期分割成数组:按数组序号分别为:年月日时分秒 302. */ 303.Date.prototype.toArray = function() 304.{ 305. var myArray = new Array(); 306. myArray[0] = this.getFullYear(); 307. myArray[1] = this.getMonth(); 308. myArray[2] = this.getDate(); 309. myArray[3] = this.getHours(); 310. myArray[4] = this.getMinutes(); 311. myArray[5] = this.getSeconds(); 312. return myArray; 313.} 314. 315. 316./** 317. * 取得日期数据信息: 318. * field:可选,标识是在哪个字段上进行比较,字段见如下的约定。无此参数时,认为d 319. * (1)Y/y 年 320. * (2)M 月 321. * (3)W/w 周 322. * (4)D/d 日 323. * (5)H/h 时 324. * (6)m 分 325. * (7)S/s 秒 326. */ 327.Date.prototype.datePart = function(field) 328.{ 329. if(!field){ 330. field = "d"; 331. } 332. var Week = ['日','六']; 333. switch (field){ 334. case 'Y' : 335. case 'y' :return this.getFullYear();break; 336. case 'M' :return (this.getMonth()+1);break; 337. case 'W' : 338. case 'w' :return Week[this.getDay()];break; 339. case 'D' : 340. case 'd' :return this.getDate();break; 341. case 'H' : 342. case 'h' :return this.getHours();break; 343. case 'm' :return this.getMinutes();break; 344. case 's' :return this.getSeconds();break; 345. default:return this.getDate(); 346. } 347. return this.getDate(); 348.}

以上这篇javascript中对Date类型的常用操作小结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

原文地址:https://www.jb51.cc/js/48646.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐