Browse Source

excel导入日期时间戳格式转换

Luolei 6 years atrás
parent
commit
6c7eff13a7
2 changed files with 26 additions and 3 deletions
  1. 14 2
      src/components/AddInspect/index.jsx
  2. 12 1
      src/utils/tools.js

+ 14 - 2
src/components/AddInspect/index.jsx

@@ -1,6 +1,6 @@
 import React from 'react';
 import { SearchOption, InspectCommon, Calendar ,Notify,ConfirmModal,Add} from '@commonComp';
-import { deepClone,normalVal } from '@utils/tools';
+import { deepClone,normalVal,timestampToTime,filterDataArr } from '@utils/tools';
 import styles from './index.less';
 import date1 from './img/date1.png';
 import date2 from './img/date2.png';
@@ -253,6 +253,18 @@ class Inspect extends React.Component {
             return <td style={{width:'20%'}}><span className={(val.value - 0).toString() == 'NaN'?"red":(val.maxValue || val.minValue) ? (val.value > val.maxValue || val.value < val.minValue?"red":''):''}>{val.value}</span> { val.labelSuffix}</td>
         }
     }
+    toTime(time){
+      let tmpTim = time.split(',').join('')-0
+      if(time && tmpTim.toString() != 'NaN'){
+        let date = new Date('1900-01-01');
+        let dateTim = date.getTime();
+        let times = (tmpTim-2)*24*60*60*1000;
+        let result = timestampToTime(dateTim+times).split(' ')[0]
+        return result;
+      }else{
+        return time;
+      }
+    }
     render() {
         const { handleChangeValue, list, labelList,delPartItem, handleLabelSub, handleClear, handleConfirm, fillActive,getExcelDataList,handleCloseExcel,handlePush } = this.props;
         const {toastText,visible} = this.state;
@@ -280,7 +292,7 @@ class Inspect extends React.Component {
                                                                 <td style={{width:'25%'}}>
                                                                     {normalVal(value.min,value.max)}
                                                                 </td>
-                                                                <td style={{width:'25%'}}>{value.time == ''?('导入时间: '+this.state.dateTime):'化验时间: '+value.time}</td>
+                                                                <td style={{width:'25%'}}>{value.time == ''?('导入时间: '+this.state.dateTime):'化验时间: '+this.toTime(value.time)}</td>
                                                             </tr>
                                                         })
                                                     }

+ 12 - 1
src/utils/tools.js

@@ -645,6 +645,16 @@ function getPageCoordinate(event){
     }
     return obj;
 }
+function timestampToTime(timestamp) {     //excel导入2019年5月1日会转换成时间戳
+  var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
+  var Y = date.getFullYear() + '-';
+  var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
+  var D = date.getDate() + ' ';
+  var h = date.getHours() + ':';
+  var m = date.getMinutes() + ':';
+  var s = date.getSeconds();
+  return Y+M+D+h+m+s;
+}
 module.exports = {
     checkType: Type.checkType,
     regexp,
@@ -670,5 +680,6 @@ module.exports = {
     isAllClear,
     normalVal,
     getPageCoordinate,
-    windowRemoveEventHandler
+    windowRemoveEventHandler,
+    timestampToTime
 };