将 Google 电子表格日期转换为 JS 日期对象? [英] Converting Google spreadsheet date into a JS Date object?
问题描述
我一直在绕圈子...我有一个包含两个日期的电子表格,我需要找到两者之间经过的年数(即某人在给定日期的年龄; 这是 Excel 的 DATEDIF 的替代品).
I've been going round in circles on this one... I've got a spreadsheet which holds two dates, and I need to find the number of elapsed years between the two (ie. someone's age at a given date; this is a replacement for Excel's DATEDIF).
第一步是将 Google 的序列号转换为 JS Date 对象,但似乎没有 Date 构造函数执行此操作.有什么想法吗?
The first step is to convert Google's serial number into a JS Date object, but there doesn't appear to be Date constructor that does this. Any ideas?
谢谢.
推荐答案
将 Google 电子表格日期转换为 javascript 日期:
To convert a Google spreadsheet date to a javascript date :
var JSdate = Date.parse(Cell.getValue())
要将 javascript 日期转换为 Google 电子表格日期:
To convert a javascript date to a Google spreadsheet date:
function GoogleDate( JSdate ) {
var D = new Date(JSdate) ;
var Null = new Date(Date.UTC(1899,11,30,0,0,0,0)) ; // the starting value for Google
return ((D.getTime() - Null.getTime())/60000 - D.getTimezoneOffset()) / 1440 ;
}
这篇关于将 Google 电子表格日期转换为 JS 日期对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!