简体   繁体   English

将Excel时间转换为moment.js

[英]Converting Excel Time to moment.js

I have an Electron app where an Excel-Sheet with a couple of columns containing time values needs to be imported. 我有一个Electron应用程序,其中需要导入带有两列包含时间值的ExcelSheet。 In my app those values are converted in a loop to momentjs object for further manipulation: 在我的应用程序中,这些值在循环中转换为momentjs对象,以进行进一步处理:

x['Time'] = moment(x['Time'], ['HH:mm','HH:mm:ss']).format('HH:mm:ss');

This works fine as long the Excel contains time values formatted as text. 只要Excel包含格式化为文本的时间值,此方法就可以正常工作。 But if the Excel is set up the way it's meant to be, then the value of the Cell is a Number between 0 and 1 (Excel counts time internally as floating point - so eg 0,5 translates to 12:00:00). 但是,如果按原本的方式设置Excel,则单元格的值就是0到1之间的数字(Excel在内部将时间作为浮点计数-例如0,5转换为12:00:00)。

Does anyone know how I can translate that back to a readable Timevalue for momentjs? 有谁知道我如何将其转换回momentjs可读的Timevalue?

This is as far as I have work with the Excel time decimal values. 据我所知,这与Excel时间十进制值有关。 So according to Excel the time text is represented by a decimal number ranging from 0 to 1. 因此,根据Excel ,时间文本由0到1的十进制数字表示。

function excelDateToJSDate(excel_date, time = false) {
  let day_time = excel_date % 1
  let meridiem = "AMPM"
  let hour = Math.floor(day_time * 24)
  let minute = Math.floor(Math.abs(day_time * 24 * 60) % 60)
  let second = Math.floor(Math.abs(day_time * 24 * 60 * 60) % 60)
  hour >= 12 ? meridiem = meridiem.slice(2, 4) : meridiem = meridiem.slice(0, 2)
  hour > 12 ? hour = hour - 12 : hour = hour
  hour = hour < 10 ? "0" + hour : hour
  minute = minute < 10 ? "0" + minute : minute
  second = second < 10 ? "0" + second : second
  let daytime = "" + hour + ":" + minute + ":" + second + " " + meridiem
  return time ? daytime : (new Date(0, 0, excel_date, 0, -new Date(0).getTimezoneOffset(), 0)).toLocaleDateString(navigator.language, {}) + " " + daytime
};

First we define the midday, then handle the hours, minutes and seconds, then verify if the given hour is either AM or PM, as a formatting fashion preference we change the 24 hours to 12 hour convention and add padding zeros to any value less than 10 and lastly return the time or date as a string. 首先我们定义中午,然后处理小时,分钟和秒,然后验证给定的小时是AM还是PM,作为一种格式化方式首选项,我们将24小时制更改为12小时制,并将填充零添加到小于10,最后以字符串形式返回时间或日期。

Example

 function excelDateToJSDate(excel_date, time = false) { let day_time = excel_date % 1 let meridiem = "AMPM" let hour = Math.floor(day_time * 24) let minute = Math.floor(Math.abs(day_time * 24 * 60) % 60) let second = Math.floor(Math.abs(day_time * 24 * 60 * 60) % 60) hour >= 12 ? meridiem = meridiem.slice(2, 4) : meridiem = meridiem.slice(0, 2) hour > 12 ? hour = hour - 12 : hour = hour hour = hour < 10 ? "0" + hour : hour minute = minute < 10 ? "0" + minute : minute second = second < 10 ? "0" + second : second let daytime = "" + hour + ":" + minute + ":" + second + " " + meridiem return time ? daytime : (new Date(0, 0, excel_date, 0, -new Date(0).getTimezoneOffset(), 0)).toLocaleDateString(navigator.language, {}) + " " + daytime }; console.log(excelDateToJSDate(0.125, true)); console.log(excelDateToJSDate(43556)); 

export const parseDateExcel = (excelTimestamp) => {
    const secondsInDay = 24 * 60 * 60;
    const excelEpoch = new Date(1899, 11, 31);
    const excelEpochAsUnixTimestamp = excelEpoch.getTime();
    const missingLeapYearDay = secondsInDay * 1000;
    const delta = excelEpochAsUnixTimestamp - missingLeapYearDay;
    const excelTimestampAsUnixTimestamp = excelTimestamp * secondsInDay * 1000;
    const parsed = excelTimestampAsUnixTimestamp + delta;
    return isNaN(parsed) ? null : parsed;
};

Usage: 用法:

new Date(parseDateExcel(36902.49097)) //=> Thu Jan 11 2001 11:46:59 GMT+0000 (Greenwich Mean Time)

Source 资源

Due to the fact I could not find a real answer, here is one that worked for me: 由于我找不到真正的答案,因此以下是对我有用的答案:

let fromExcel = 0,709722222222222; //translates to 17:02:00
let basenumber = (fromExcel*24)
let hour = Math.floor(basenumber).toString();
if (hour.length < 2) {
    hour = '0'+hour;
}

var minute = Math.round((basenumber % 1)*60).toString();
if (minute.length < 2) {
 minute = '0'+minute;
}
let Timestring = (hour+':'+minute+':00');

So I have a String momentjs can translate. 所以我有一个String momentjs可以翻译。 The reason I do not mark this as answer is that there sure are nicer ways of conversion and I could not find a solution to calculate the seconds (which in my special case does not matter, as I do not use them). 我之所以没有将其标记为答案的原因是,肯定存在更好的转换方式,并且我找不到计算秒数的解决方案(在我的特殊情况下这无关紧要,因为我不使用它们)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM