理财公司网站模板下载,携wordpress,郑州公司网站平台建设,湖北二师网站建设排名原文链接#xff1a;http://blog.csdn.net/crper/article/details/55194334
--------------------------------------------------- 前言
今天有个接口字段需求#xff0c;要写一个今天及前几天的日期传过去#xff1b;
在网上找了下都木有什么比较好的方案#xff1b;就…原文链接http://blog.csdn.net/crper/article/details/55194334
--------------------------------------------------- 前言
今天有个接口字段需求要写一个今天及前几天的日期传过去
在网上找了下都木有什么比较好的方案就自己写了一个。
因为技术栈就是NG2TS2WEBPACK这里的代码需要一定的TS2及ES6的基础代码
/*** param {number} range* param {string} [type]* memberOf VehicleOverviewComponent* description 获取今天及前后天*/getRangeDate( range: number, type?: string ) {const formatDate ( time: any ) {// 格式化日期获取今天的日期const Dates new Date( time );const year: number Dates.getFullYear();const month: any ( Dates.getMonth() 1 ) 10 ? 0 ( Dates.getMonth() 1 ) : ( Dates.getMonth() 1 );const day: any Dates.getDate() 10 ? 0 Dates.getDate() : Dates.getDate();return year - month - day;};const now formatDate( new Date().getTime() ); // 当前时间const resultArr: Arrayany [];let changeDate: string;if ( range ) {if ( type ) {if ( type one ) {changeDate formatDate( new Date().getTime() ( 1000 * 3600 * 24 * range ) );console.log( changeDate );}if ( type more ) {if ( range 0 ) {for ( let i Math.abs( range ); i 0; i-- ) {resultArr.push( formatDate( new Date().getTime() ( -1000 * 3600 * 24 * i ) ) );console.log( resultArr );}} else {for ( let i 1; i range; i ) {resultArr.push( formatDate( new Date().getTime() ( 1000 * 3600 * 24 * i ) ) );console.log( resultArr );}}}} else {changeDate formatDate( new Date().getTime() ( 1000 * 3600 * 24 * range ) );console.log( changeDate );}}}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546471234567891011121314151617181920212223242526272829303132333435363738394041424344454647 调用及结果
range参数支持正负数里面也加了判断;type【为可选参数】有两种一个是字符串one一个是more前者返回一个指定的日期后者返回一个排序好的范围 getRangeDate( -6 );// 结果2017-02-09getRangeDate( -6, one );// 结果2017-02-091212getRangeDate( -6, more );// 结果// [2017-02-09, 2017-02-10, 2017-02-11, 2017-02-12, 2017-02-13, 2017-02-14, 2017-02-15]123123 总结 就是用时间戳进行换算然后通过内置函数获取对应字段进行拼接这里没有带时分秒有兴趣的可以加个可选参数把时分秒带上。。因为我这里不需要用到所以我就没加进去了。。 结果集为数组但不仅限于数组也可以改成对象。。看你们喜欢啦