Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript中格式化时间戳以显示相关时区中的正确时间_Javascript_Firebase_Google Cloud Functions_Datetime Format - Fatal编程技术网

如何在Javascript中格式化时间戳以显示相关时区中的正确时间

如何在Javascript中格式化时间戳以显示相关时区中的正确时间,javascript,firebase,google-cloud-functions,datetime-format,Javascript,Firebase,Google Cloud Functions,Datetime Format,我对JavaScript的处理时间有问题。我在firebase的一个文档中有一个时间戳,我有一个云函数,它应该发送一个通知。我想发送时间戳格式正确的通知,作为英国的当前时区(当前为BST、UTC+1或GMT+1)。下面是我的代码 exports.sendNotificationNewRota = functions.firestore .document('rota/{attendanceId}') .onCreate(async snapshot => { const t

我对JavaScript的处理时间有问题。我在firebase的一个文档中有一个时间戳,我有一个云函数,它应该发送一个通知。我想发送时间戳格式正确的通知,作为英国的当前时区(当前为BST、UTC+1或GMT+1)。下面是我的代码

exports.sendNotificationNewRota = functions.firestore
  .document('rota/{attendanceId}')
  .onCreate(async snapshot => {
    const transaction = snapshot.data();

    var dateIn = transaction.timeIn.toDate();

    let timeIn = dateIn.toLocaleTimeString( {
      timezone: 'Europe/London',
      timeZoneName: 'long',
      hour: '2-digit',
      minute:'2-digit'});

    console.log(timeIn);
这段代码的输出给我一个UTC时间。这可能是罚款时,英国夏令时将完成,但不是现在。有没有办法妥善处理时间


谢谢

请注意
Date.prototype.toLocaleTimeString()的函数签名

详情如下

您可以有效地将配置传递给
locales
参数,要使代码正常工作,您需要添加一个空的第一个参数。或者,您也可以将其指定为
'en-UK'
,例如:

exports.sendNotificationNewRota=functions.firestore
.document('rota/{attendanceId}'))
.onCreate(异步快照=>{
const transaction=snapshot.data();
var dateIn=transaction.timeIn.toDate();

让timeIn=dateIn.toLocaleTimeString([],{/注意
Date.prototype.toLocaleTimeString()的函数签名

详情如下

您可以有效地将配置传递给
locales
参数,要使代码正常工作,您需要添加一个空的第一个参数。或者,您也可以将其指定为
'en-UK'
,例如:

exports.sendNotificationNewRota=functions.firestore
.document('rota/{attendanceId}'))
.onCreate(异步快照=>{
const transaction=snapshot.data();
var dateIn=transaction.timeIn.toDate();

让timeIn=dateIn.toLocaleTimeString([],{/天啊,我在这件事上浪费了很多时间,但最终我意识到这是我代码中的一个输入错误。任何有相同问题的人都要确保使用
时区
而不是
时区

天啊,我在这件事上浪费了很多时间,但最终我意识到这是我代码中的一个输入错误。任何有相同问题的人都要确保使用
timeZone
而不是
时区

您的代码明确请求特定时区。如果不这样做,您将获得客户端实际时区的时间格式。如果我删除时区:“Europe/London”,当我键入
new Date().toLocaleTimeString()时,它仍然会给我UTC时间
在我的浏览器中,我获得了正确的本地时区(美国“中央”夏令时)的时间。您没有正确调用toLocaleTimeString。例如,使用。您的代码显式请求特定时区。如果不这样做,您将获得客户端实际时区的时间格式。如果删除时区:“Europe/London”,当我键入
new Date()。toLocaleTimeString()
在我的浏览器中,我在正确的本地时区(美国“中央”夏令时)中获取时间。您没有正确调用toLocaleTimeString。请使用示例。我已尝试使用[],使用“en-UK”和“en-GB”,但我仍然使用UTC时间。这是我在控制台中得到的时间:协调世界时间上午11:00。我希望英国夏季时间下午12点。我想可能问题在于,该代码在云函数中运行……如果我在浏览器中运行,它工作正常……我已经尝试使用[],使用“en UK”和“en GB”,但我仍然使用UTC时间。这是我在控制台中得到的时间:协调世界时上午11:00。我希望英国夏季时间下午12:00。我想可能问题在于,此代码在云函数中运行……如果我在浏览器中运行,它工作正常。。。
dateObj.toLocaleTimeString([locales[, options]])