Google Apps脚本_将浮点数转换为日期 [英] Google Apps Script _ Convert float to date

查看:83
本文介绍了Google Apps脚本_将浮点数转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的电子表格中有一个单元格,其值为5/31/2016 0:22:38,但是当我编写脚本来获取数据时,它返回42521.015713761575。我已经尝试了一些来自 jquery转换日期的代码?的代码,但大多数返回日期到1970年。我将如何能够将上述浮点格式化为上面指定的日期?谢谢......)

I have a cell in my google spreadsheet with the value of 5/31/2016 0:22:38, but when I wrote a script to fetch the data it returns 42521.015713761575. I've tried some codes from jquery convert number to date? but most returns dates back to 1970. How would I be able to format the said float into the date specified above? thanks... :)

推荐答案

这显然是自1899年12月30日以来00:00:00 。

This is apparently a number of days elapsed since December 30, 1899, 00:00:00.

您可以使用以下代码将其重新转换为UTC日期(请注意,月份为零,因此12月= 11):

You can convert it back to a UTC date with the following code (note that months are zero-based, so December = 11):

var x = 42521.015713761575;

// seconds in day = 24 * 60 * 60 = 86400
var date = new Date(Date.UTC(1899, 11, 30, 0, 0, x * 86400));

console.log(date.toUTCString());

这篇关于Google Apps脚本_将浮点数转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆