Pārlūkot izejas kodu

日期转换bug

luolei 5 gadi atpakaļ
vecāks
revīzija
e932aa12dc

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

@@ -2,7 +2,7 @@ import React from 'react';
 import { SearchOption, InspectCommon, Calendar, Notify, ConfirmModal, Add, DelToast } from '@commonComp';
 import SlideExcel from './SlideExcel';
 import SlideSelect from './SlideSelect';
-import { deepClone, getPageCoordinate, getStatusImg ,getCurrentDate,setPosition} from '@utils/tools';
+import { deepClone, getPageCoordinate, getStatusImg ,getCurrentDate,setPosition,getCalendarDate} from '@utils/tools';
 import styles from './index.less';
 import ScrollArea from 'react-scrollbar';
 import store from '@store';
@@ -126,8 +126,7 @@ class Inspect extends React.Component {
     this.props.setHighter(48)
   }
   handleChangeDate(info) {
-    // let date = info.year+'-'+info.month.toString().padStart(2,'0')+'-'+info.day.toString().padStart(2,'0')
-    let date = info.year+'-'+info.month.toString().padStart(2,'0')+'-'+info.day.toString().padStart(2,'0')+' '+info.hour.toString().padStart(2,'0')+':'+info.minute.toString().padStart(2,'0')+':'+info.second.toString().padStart(2,'0');
+    let date = getCalendarDate(info);
     this.setState({ dateTime: date, date: false })
   }
   handleSearchShow(e) {

+ 10 - 13
src/components/TimeInterval/index.jsx

@@ -59,14 +59,13 @@ class TimeInterval extends React.Component {
   timeSure(date) {
     const { getStartTime, getEndTime } = this.props;
     if (this.state.flg == 1) {
-      let time = getCalendarDate(date, true);
-      let dateBegin = (new Date(time)).getTime();
+      let time = getCalendarDate(date);
+      let tmpTime = time.replace(/ |:/g, '-').split('-');
       let arr = this.state.endTime.replace(/ |:/g, '-').split('-');
-      let date11 = new Date(Date.UTC(arr[0], arr[1], arr[2], arr[3],arr[4]));
-      // let dateEnd = (new Date(this.state.endTime)).getTime()
-      let dateEnd = date11.getTime()
+      let dateBegin = (new Date(Date.UTC(tmpTime[0],tmpTime[1],tmpTime[2],tmpTime[3],tmpTime[4]))).getTime();
+      let dateEnd = (new Date(Date.UTC(arr[0], arr[1], arr[2], arr[3],arr[4]))).getTime();
       let dateDes = (dateEnd - dateBegin)/(24 * 3600 * 1000)
-      if(dateEnd < dateBegin){
+      if(dateEnd - dateBegin < 0){
         Notify.info("终止时间不能早于起始时间");
         return
       }else if(dateDes>90){
@@ -79,16 +78,14 @@ class TimeInterval extends React.Component {
         timeLisStart: date//事件组件的日期
       })
       getStartTime(time)
-
     } else if (this.state.flg == 2) {
-      let time = getCalendarDate(date, true);
-      // let dateBegin = (new Date(this.state.startTime)).getTime()
+      let time = getCalendarDate(date);
+      let tmpTime = time.replace(/ |:/g, '-').split('-');
       let arr = this.state.endTime.replace(/ |:/g, '-').split('-');
-      let date11 = new Date(Date.UTC(arr[0], arr[1], arr[2], arr[3],arr[4]));
-      let dateBegin = date11.getTime()
-      let dateEnd = (new Date(time)).getTime()
+      let dateBegin = (new Date(Date.UTC(arr[0], arr[1], arr[2], arr[3],arr[4]))).getTime();
+      let dateEnd = (new Date(Date.UTC(tmpTime[0],tmpTime[1],tmpTime[2],tmpTime[3],tmpTime[4]))).getTime();
       let dateDes = (dateEnd - dateBegin)/(24 * 3600 * 1000)
-      if(dateEnd < dateBegin){
+      if(dateEnd - dateBegin < 0){
         Notify.info("终止时间不能早于起始时间");
         return
       }else if(dateDes>90){

+ 2 - 1
src/containers/AssistCheck.js

@@ -7,6 +7,7 @@ import { assistLable,delAssistLabel,changeAssistVal,changeDate,allCheckImports,s
 import { getSearchList,getInstroduce ,getImportLists,getSonDetailList} from '@store/async-actions/assistCheck';
 import {ISREAD} from '@store/types/homePage.js';
 import {billing, getConceptDetail} from '@store/async-actions/pushMessage';
+import { getCalendarDate} from '@utils/tools';
 
 function mapStateToProps(state) {//console.log(state)
     return {
@@ -67,7 +68,7 @@ function mapDispatchToProps(dispatch, store) {
             dispatch(changeAssistVal(val,idx))
         },
         handleChangeDate(info,idx){
-            let date = info.year+'-'+info.month.toString().padStart(2,'0')+'-'+info.day.toString().padStart(2,'0')+' '+info.hour.toString().padStart(2,'0')+':'+info.minute.toString().padStart(2,'0')+':'+info.second.toString().padStart(2,'0');
+            let date = getCalendarDate(info);
             dispatch(changeDate(date,idx))
         },
         //右侧推送

+ 9 - 16
src/utils/tools.js

@@ -817,9 +817,9 @@ function getCurrentDate(flg,con){
     var h = myDate.getHours();
     var m = myDate.getMinutes();
     var s = myDate.getSeconds();
-    date = year+link+mon.toString().padStart(2,'0')+link+day.toString().padStart(2,'0')+' '+h.toString().padStart(2,'0')+':'+m.toString().padStart(2,'0')+':'+s.toString().padStart(2,'0');
+    date = year+link+(mon<10?"0"+mon:mon)+link+(day<10?"0"+day:day)+' '+(h<10?"0"+h:h)+':'+(m<10?"0"+m:m)+':'+(s<10?"0"+s:s);
   }else{
-    date = year+link+mon.toString().padStart(2,'0')+link+day.toString().padStart(2,'0');
+    date = year+link+(mon<10?"0"+mon:mon)+link+(day<10?"0"+day:day);
   }
   return date;
 }
@@ -925,11 +925,11 @@ function dateParser(timestamp,link = '-'){
   let year = time.getFullYear();
   let month = time.getMonth()+1;
   let date = time.getDate();
-  let hour = time.getHours().toString().padStart(2,'0');
-  let minute = time.getMinutes().toString().padStart(2,'0');
-  let second = time.getSeconds().toString().padStart(2,'0');
+  let hour = time.getHours();
+  let minute = time.getMinutes();
+  let second = time.getSeconds();
   // let result = year+link+month+link+date;
-  let result = year+link+(month<10?"0"+month:month)+link+(date<10?"0"+date:date)+' '+hour+':'+minute+':'+second;
+  let result = year+link+(month<10?"0"+month:month)+link+(date<10?"0"+date:date)+' '+(hour<10?"0"+hour:hour)+':'+(minute<10?"0"+minute:minute)+':'+(second<10?"0"+second:second);
   return result;
 }
 
@@ -1175,16 +1175,9 @@ function getIds(data){
  * @param {时间是否包含时分秒} flg 
  * @param {连接符} con 
  */
-function getCalendarDate(info,flg,con) {
-  let date = '';
-  let time = flg?flg:false;
-  let conect = con?con:'-';
-  //FF26和IE不兼容padStart
-  /*if(time){
-    date = info.year + conect + (info.month).toString().padStart(2,'0') + conect + (info.day).toString().padStart(2,'0') + ' '+(info.hour).toString().padStart(2,'0')+':'+(info.minute).toString().padStart(2,'0')+':'+(info.second).toString().padStart(2,'0');
-  }else{*/
-    date = info.year + conect + (info.month < 10 ? '0' + info.month : info.month) + conect + (info.day < 10 ? '0' + info.day : info.day) + ' '+ (info.hour < 10 ? '0' + info.hour : info.hour)+':'+ (info.minute < 10 ? '0' + info.minute : info.minute)+':'+ (info.second < 10 ? '0' + info.second : info.second);
-  // }
+function getCalendarDate(info) {
+  let conect = '-';
+  let date = info.year + conect + (info.month < 10 ? '0' + info.month : info.month) + conect + (info.day < 10 ? '0' + info.day : info.day) + ' '+ (info.hour < 10 ? '0' + info.hour : info.hour)+':'+ (info.minute < 10 ? '0' + info.minute : info.minute)+':'+ (info.second < 10 ? '0' + info.second : info.second);
   return date;
 }