简体   繁体   English

如何在 Javascript 中将 UTC/GMT 日期时间转换为 CST? (不是本地的,CST 总是)

[英]How do I convert UTC/ GMT datetime to CST in Javascript? (not local, CST always)

I have a challenge where backend data is always stored in UTC time.我有一个挑战,后端数据总是以 UTC 时间存储。 Our front-end data is always presented in CST.我们的前端数据始终以 CST 格式呈现。 I don't have access to this 'black box.'我无权访问这个“黑匣子”。

I would like to mirror this in our data warehouse.我想在我们的数据仓库中反映这一点。 Which is based in Europe (CET).它位于欧洲 (CET)。 So "local" conversion will not work.所以“本地”转换将不起作用。

I'm wondering the simplest, most straightforward way to accurately convert UTC time (I can have it in epoch milliseconds or a date format '2015-01-01 00:00:00') to Central Standard Time.我想知道将 UTC 时间(我可以使用纪元毫秒或日期格式“2015-01-01 00:00:00”)准确转换为中央标准时间的最简单、最直接的方法。 (which is 5 or 6 hours behind based on Daylight Savings). (根据夏令时,晚了 5 或 6 小时)。

I see a lot of threads about converting to 'local' time... again I don't want this, nor do I simply want to subtract 6 hours which will be wrong half the year.我看到很多关于转换为“本地”时间的话题……我不想这样,我也不想简单地减去 6 个小时,这将是半年的错误。

Anyone have any ideas?有人有主意吗? This seems to be a very common problem but I've been searching for a while, and have found nothing.这似乎是一个非常普遍的问题,但我已经搜索了一段时间,但一无所获。

I have a challenge where backend data is always stored in UTC time.我有一个挑战,后端数据总是以 UTC 时间存储。 Our front-end data is always presented in CST.我们的前端数据始终以 CST 形式呈现。 I don't have access to this 'black box.'我无权访问这个“黑匣子”。

I would like to mirror this in our data warehouse.我想在我们的数据仓库中反映这一点。 Which is based in Europe (CET).其总部位于欧洲(CET)。 So "local" conversion will not work.所以“本地”转换将不起作用。

I'm wondering the simplest, most straightforward way to accurately convert UTC time (I can have it in epoch milliseconds or a date format '2015-01-01 00:00:00') to Central Standard Time.我想知道将UTC时间(我可以以纪元毫秒或日期格式'2015-01-01 00:00:00')准确转换为中央标准时间的最简单、最直接的方法。 (which is 5 or 6 hours behind based on Daylight Savings). (根据夏令时,晚了 5 或 6 小时)。

I see a lot of threads about converting to 'local' time ... again I don't want this, nor do I simply want to subtract 6 hours which will be wrong half the year.我看到很多关于转换为“本地”时间的帖子……我又不想要这个,我也不想减去 6 个小时,这在半年内都是错误的。

Anyone have any ideas?有人有主意吗? This seems to be a very common problem but I've been searching for a while, and have found nothing.这似乎是一个非常普遍的问题,但我已经搜索了一段时间,但一无所获。

I have a challenge where backend data is always stored in UTC time.我有一个挑战,后端数据总是以 UTC 时间存储。 Our front-end data is always presented in CST.我们的前端数据始终以 CST 形式呈现。 I don't have access to this 'black box.'我无权访问这个“黑匣子”。

I would like to mirror this in our data warehouse.我想在我们的数据仓库中反映这一点。 Which is based in Europe (CET).其总部位于欧洲(CET)。 So "local" conversion will not work.所以“本地”转换将不起作用。

I'm wondering the simplest, most straightforward way to accurately convert UTC time (I can have it in epoch milliseconds or a date format '2015-01-01 00:00:00') to Central Standard Time.我想知道将UTC时间(我可以以纪元毫秒或日期格式'2015-01-01 00:00:00')准确转换为中央标准时间的最简单、最直接的方法。 (which is 5 or 6 hours behind based on Daylight Savings). (根据夏令时,晚了 5 或 6 小时)。

I see a lot of threads about converting to 'local' time ... again I don't want this, nor do I simply want to subtract 6 hours which will be wrong half the year.我看到很多关于转换为“本地”时间的帖子……我又不想要这个,我也不想减去 6 个小时,这在半年内都是错误的。

Anyone have any ideas?有人有主意吗? This seems to be a very common problem but I've been searching for a while, and have found nothing.这似乎是一个非常普遍的问题,但我已经搜索了一段时间,但一无所获。

I have a challenge where backend data is always stored in UTC time.我有一个挑战,后端数据总是以 UTC 时间存储。 Our front-end data is always presented in CST.我们的前端数据始终以 CST 形式呈现。 I don't have access to this 'black box.'我无权访问这个“黑匣子”。

I would like to mirror this in our data warehouse.我想在我们的数据仓库中反映这一点。 Which is based in Europe (CET).其总部位于欧洲(CET)。 So "local" conversion will not work.所以“本地”转换将不起作用。

I'm wondering the simplest, most straightforward way to accurately convert UTC time (I can have it in epoch milliseconds or a date format '2015-01-01 00:00:00') to Central Standard Time.我想知道将UTC时间(我可以以纪元毫秒或日期格式'2015-01-01 00:00:00')准确转换为中央标准时间的最简单、最直接的方法。 (which is 5 or 6 hours behind based on Daylight Savings). (根据夏令时,晚了 5 或 6 小时)。

I see a lot of threads about converting to 'local' time ... again I don't want this, nor do I simply want to subtract 6 hours which will be wrong half the year.我看到很多关于转换为“本地”时间的帖子……我又不想要这个,我也不想减去 6 个小时,这在半年内都是错误的。

Anyone have any ideas?有人有主意吗? This seems to be a very common problem but I've been searching for a while, and have found nothing.这似乎是一个非常普遍的问题,但我已经搜索了一段时间,但一无所获。

Maybe you can use something like the following.也许您可以使用以下内容。 Note, that is just an example you might need to adjust it to your needs.请注意,这只是一个示例,您可能需要根据需要对其进行调整。

    let cstTime = new Date(createdAt).toLocaleString("es-MX", {
    timeZone: "America/Mexico_City" });

You can use below code snippet您可以使用下面的代码片段

 // Get time zone offset for CDT or CST const getCdtCstOffset = () => { const getNthSunday = (date, nth) => { date.setDate((7*(nth-1))+(8-date.getDay())); return date; } const isCdtTimezoneOffset = (today) => { console.log('Today: ', today); let dt = new Date(); var mar = new Date(dt.getFullYear(), 2, 1); mar = getNthSunday(mar, 2); console.log('CDT Start: ', mar); var nov = new Date(dt.getFullYear(), 10, 1, 23, 59, 59); nov = getNthSunday(nov, 1); console.log('CDT End: ', nov); return mar.getTime()< today.getTime() && nov.getTime()> today.getTime(); } var today = new Date()// current date if (isCdtTimezoneOffset(today)) { return -5 } else { return -6 } } let cstOrCdt = new Date(); cstOrCdt.setHours(cstOrCdt.getHours()+getCdtCstOffset()) console.log('CstOrCdt: ', cstOrCdt);

I have a challenge where backend data is always stored in UTC time.我有一个挑战,后端数据总是以 UTC 时间存储。 Our front-end data is always presented in CST.我们的前端数据始终以 CST 形式呈现。 I don't have access to this 'black box.'我无权访问这个“黑匣子”。

I would like to mirror this in our data warehouse.我想在我们的数据仓库中反映这一点。 Which is based in Europe (CET).其总部位于欧洲(CET)。 So "local" conversion will not work.所以“本地”转换将不起作用。

I'm wondering the simplest, most straightforward way to accurately convert UTC time (I can have it in epoch milliseconds or a date format '2015-01-01 00:00:00') to Central Standard Time.我想知道将UTC时间(我可以以纪元毫秒或日期格式'2015-01-01 00:00:00')准确转换为中央标准时间的最简单、最直接的方法。 (which is 5 or 6 hours behind based on Daylight Savings). (根据夏令时,晚了 5 或 6 小时)。

I see a lot of threads about converting to 'local' time ... again I don't want this, nor do I simply want to subtract 6 hours which will be wrong half the year.我看到很多关于转换为“本地”时间的帖子……我又不想要这个,我也不想减去 6 个小时,这在半年内都是错误的。

Anyone have any ideas?有人有主意吗? This seems to be a very common problem but I've been searching for a while, and have found nothing.这似乎是一个非常普遍的问题,但我已经搜索了一段时间,但一无所获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM