No Description

common.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. function getDay (day,haveHours){
  2. //day为0表示今天的日期 -1为昨天 haveHours存在表示要时分秒 haveHours:true,false
  3. var today = new Date();
  4. var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
  5. today.setTime(targetday_milliseconds); //注意,这行是关键代码
  6. var tYear = today.getFullYear();
  7. var tMonth = today.getMonth();
  8. var tDate = today.getDate();
  9. var hours = today.getHours();
  10. var minutes = today.getMinutes(); //分
  11. var seconds = today.getSeconds(); //秒
  12. tMonth = doHandleMonth(tMonth + 1);
  13. tDate = doHandleMonth(tDate);
  14. if(haveHours){
  15. if(day == 0){
  16. return {
  17. 'now':tYear+"-"+tMonth+"-"+tDate+' ' + hours + ':' + minutes + ':' + seconds,
  18. 'zero':tYear+"-"+tMonth+"-"+tDate+' ' + '00:00:00'
  19. }
  20. }else{
  21. return tYear+"-"+tMonth+"-"+tDate+' ' + hours + ':' + minutes + ':' + seconds
  22. }
  23. }else{
  24. return tYear+"-"+tMonth+"-"+tDate
  25. }
  26. function doHandleMonth(month){
  27. var m = month;
  28. if(month.toString().length == 1){
  29. m = "0" + month;
  30. }
  31. return m;
  32. }
  33. }
  34. function NumberHandle (value,numberDigit) {//数值小数点处理 ①5997;②8.1w;③2489kw;④4.2亿 且保留一位小数
  35. if(typeof parseFloat(value) === 'number' && !isNaN(value)){
  36. //判断是否是数值类型
  37. if((value >= 10000 && value < 10000000) || (value <= -10000 && value > -10000000)){
  38. return hasDot(value/10000,numberDigit) + 'w'
  39. }else if((value >= 10000000 && value < 100000000) || (value <= -10000000 && value > -100000000)){
  40. return hasDot(value/10000000,numberDigit) + 'kw'
  41. }else if(value >= 100000000 || value <= -100000000){
  42. return hasDot(value/100000000,numberDigit) + '亿'
  43. }else{
  44. return hasDot(value,numberDigit)
  45. }
  46. }else{
  47. return value
  48. }
  49. }
  50. function hasDot(num,numberDigit){
  51. //有小数点就保留一个小数,没有就直接返回 ,默认保留1位小数
  52. var digit = 1;
  53. if(numberDigit){
  54. digit = numberDigit
  55. }
  56. return (num + '').indexOf('.') != -1 ? num.toFixed(digit) : num;
  57. }
  58. module.exports = {
  59. getDay: getDay,
  60. NumberHandle: NumberHandle,
  61. }