仅提取新日期的时间 [英] Extracting just the time for a new date

查看:62
本文介绍了仅提取新日期的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试处理日期和时间时头疼不已,并且它占用了我的编程时间,因此任何帮助都值得赞赏。

I'm having major headaches trying to work with dates and times and it's consuming hours of my programming time, so any help much appreciated.

我在做什么尝试做的是结合单独的日期和时间;轻松区分年和月。但是说到时间,它会输出:

What I'm trying to do is combine a separate date and time; It's easy enough to separate out the year and month. But when it comes to the time, it outputs:

Sat Dec 30 1899 14:30:00 GMT+1300 (NZDT)

表单字段看起来像

时间正确显示

在工作表上。

除了将整列格式化为字符串(这将破坏项目中的其他代码)?

How can I get the desired time of 13:00 from this, aside from formatting the entire column as a string (which will break other code in the project)?

推荐答案

经过大量实验,我已经找到了基本的解决方案。我希望Google会更新从Forms添加时间的方式,但是会中断。我所做的是获取时间00:00:00的毫秒值,并计算给定时间的差值,然后使用一些简单的数学运算来得出小时和分钟。

I've figured out a rudimentary solution after much experimentation. I expect if Google update how times are added from Forms it'll break though. What I've done is taken the millisecond value of a time of 00:00:00 and taken the difference with a given time, then used some simple math to get the hour and minute out of that.

function extractTime(date) {

  var milliseconds =  date.getTime()+2209203000000;
  var time = {};
  time.hours = Math.floor(milliseconds/(1000*60*60));
  time.minutes = Math.round((milliseconds/(1000*60*60)-time.hours)*60);

  return time;

}

已经过测试,似乎可以正常工作。

Have tested and it seems to work.

这篇关于仅提取新日期的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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