【问题标题】:How to get Unix time as integer format in Google Apps Script如何在 Google Apps 脚本中以整数格式获取 Unix 时间
【发布时间】:2016-12-20 13:37:47
【问题描述】:

我想在 GAS 中使用 unix 时间戳来计数随机数。

var nonce = Math.floor((date.getTime()/1000))

是我的第一个想法。但 GAS 将这种变体处理为 1.482239855E9 - 9th Gig 格式 - 而不是 1482239855
当我将它与其他字符串连接时,它显示1482239855my_string,这很好。

问题是我将这个 nonce 发送到带有加密 1482239855my_string 的服务器进行身份验证,然后 nonce 将不匹配 1.482239855E91482239855
我怎样才能得到这个随机数作为整数格式?在电子表格中,我知道range 中的setNumberFormat("0") 方法。有没有类似的方法或属性?

【问题讨论】:

  • 嗯。我使用nonce = Utilities.formatDate(new Date(), "CET", "yyyyMMddHHmmss"); 并且工作正常。还有其他方法吗??

标签: google-apps-script


【解决方案1】:

变量nonce 已经是一个整数,指数格式正是Logger.log 在屏幕上显示的方式。它使用toString(与另一个字符串连接时也隐式调用)获得的字符串表示具有普通格式,可以安全地用于信息交换。

var date = new Date();
var nonce = Math.floor((date.getTime()/1000)).toString(); // is "1482247966"

【讨论】:

  • 啊,toString方法真的很简单。谢谢扎克!!
【解决方案2】:

在 Google Apps Script 中,您可以使用以下代码获取 Unix Timestamp。

var timestamp = new Date().getTime();
Logger.log(timestamp.toString());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多