1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- function getDay (day,haveHours){
- //day为0表示今天的日期 -1为昨天 haveHours存在表示要时分秒 haveHours:true,false
- var today = new Date();
- var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
- today.setTime(targetday_milliseconds); //注意,这行是关键代码
- var tYear = today.getFullYear();
- var tMonth = today.getMonth();
- var tDate = today.getDate();
- var hours = today.getHours();
- var minutes = today.getMinutes(); //分
- var seconds = today.getSeconds(); //秒
- tMonth = doHandleMonth(tMonth + 1);
- tDate = doHandleMonth(tDate);
- if(haveHours){
- if(day == 0){
- return {
- 'now':tYear+"-"+tMonth+"-"+tDate+' ' + hours + ':' + minutes + ':' + seconds,
- 'zero':tYear+"-"+tMonth+"-"+tDate+' ' + '00:00:00'
- }
- }else{
- return tYear+"-"+tMonth+"-"+tDate+' ' + hours + ':' + minutes + ':' + seconds
- }
- }else{
- return tYear+"-"+tMonth+"-"+tDate
- }
- function doHandleMonth(month){
- var m = month;
- if(month.toString().length == 1){
- m = "0" + month;
- }
- return m;
- }
- }
- function NumberHandle (value,numberDigit) {//数值小数点处理 ①5997;②8.1w;③2489kw;④4.2亿 且保留一位小数
- if(typeof parseFloat(value) === 'number' && !isNaN(value)){
- //判断是否是数值类型
- if((value >= 10000 && value < 10000000) || (value <= -10000 && value > -10000000)){
- return hasDot(value/10000,numberDigit) + 'w'
- }else if((value >= 10000000 && value < 100000000) || (value <= -10000000 && value > -100000000)){
- return hasDot(value/10000000,numberDigit) + 'kw'
- }else if(value >= 100000000 || value <= -100000000){
- return hasDot(value/100000000,numberDigit) + '亿'
- }else{
- return hasDot(value,numberDigit)
- }
- }else{
- return value
- }
- }
- function hasDot(num,numberDigit){
- //有小数点就保留一个小数,没有就直接返回 ,默认保留1位小数
- var digit = 1;
- if(numberDigit){
- digit = numberDigit
- }
- return (num + '').indexOf('.') != -1 ? num.toFixed(digit) : num;
- }
- module.exports = {
- getDay: getDay,
- NumberHandle: NumberHandle,
- }
|