VBA本地时区调整

2024-03-06

假设我有一个本地太平洋时间 (UTC-8) Excel 值。例如41656.67297 is 17 January 2014 4:09 PM。我无法控制这个值。

我想相对于用户当地时间调整给定值。例如,都柏林的某人正在使用 Excel 工作簿。我想调整给定的数字并以他的当地时间显示它,18 January 2014 12:09 AM。有没有内置的 Excel 功能可以让这一切顺利进行?


VBA 本身不提供此选项,但有一个 Windows API 可以让您完成您想要的操作。本文 http://www.cpearson.com/excel/TimeZoneAndDaylightTime.aspx描述了如何做到这一点以及here http://msdn.microsoft.com/en-us/library/ms885646.aspxMSDN 页面位于GetTimeZoneInformation.

在 CPearson 页面上,首先进行以​​下声明:

Option Explicit

Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' NOTE: If you are using the Windows WinAPI Viewer Add-In to get
' function declarations, not that there is an error in the 
' TIME_ZONE_INFORMATION structure. It defines StandardName and
' DaylightName As 32. This is fine if you have an Option Base
' directive to set the lower bound of arrays to 1. However, if 
' your Option Base directive is set to 0 or you have no 
' Option Base diretive, the code won't work. Instead,
' change the (32) to (0 To 31).
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Type TIME_ZONE_INFORMATION
    Bias As Long
    StandardName(0 To 31) As Integer
    StandardDate As SYSTEMTIME
    StandardBias As Long
    DaylightName(0 TO 31) As Integer
    DaylightDate As SYSTEMTIME
    DaylightBias As Long
End Type


''''''''''''''''''''''''''''''''''''''''''''''
' These give symbolic names to the time zone
' values returned by GetTimeZoneInformation .
''''''''''''''''''''''''''''''''''''''''''''''

Private Enum TIME_ZONE
    TIME_ZONE_ID_INVALID = 0        ' Cannot determine DST
    TIME_ZONE_STANDARD = 1          ' Standard Time, not Daylight
    TIME_ZONE_DAYLIGHT = 2          ' Daylight Time, not Standard
End Enum


Private Declare Function GetTimeZoneInformation Lib "kernel32" _
    (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

Private Declare Sub GetSystemTime Lib "kernel32" _
    (lpSystemTime As SYSTEMTIME)

然后这个函数将找到用户的本地时间和与 GMT 的偏移量:

Function LocalOffsetFromGMT(Optional AsHours As Boolean = False, _
    Optional AdjustForDST As Boolean = False) As Double
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' LocalOffsetFromGMT
' This returns the amount of time in minutes (if AsHours is omitted or
' false) or hours (if AsHours is True) that should be *added* to the
' local time to get GMT. If AdjustForDST is missing or false,
' the unmodified difference is returned. (e.g., Kansas City to London
' is 6 hours normally, 5 hours during DST. If AdjustForDST is False,
' the resultif 6 hours. If AdjustForDST is True, the result is 5 hours
' if DST is in effect.)
' Note that the return type of the function is a Double not a Long. This
' is to accomodate those few places in the world where the GMT offset 
' is not an even hour, such as Newfoundland, Canada, where the offset is
' on a half-hour displacement.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim TBias As Long
Dim TZI As TIME_ZONE_INFORMATION
Dim DST As TIME_ZONE
DST = GetTimeZoneInformation(TZI)

If DST = TIME_ZONE_DAYLIGHT Then
    If AdjustForDST = True Then
        TBias = TZI.Bias + TZI.DaylightBias
    Else
        TBias = TZI.Bias
    End If
Else
    TBias = TZI.Bias
End If
If AsHours = True Then
    TBias = TBias / 60
End If

LocalOffsetFromGMT = TBias

End Function

您可以使用以下方法测试代码:

MsgBox (LocalOffsetFromGMT())

然后要将此偏移量添加到原始时间,您可以执行以下操作:

OriginalTime + TIME(LocalOffsetFromGMT(True, True), 0, 0)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VBA本地时区调整 的相关文章

  • 在用户窗体终止/关闭 VBA 时调用数组

    我有一个问题 我想在用户窗体关闭时将用户窗体的内容存储在数组中 我认为我的语法正确 但似乎不会在用户窗体初始化时重新填充 我尝试将数组放入其自己的模块中 但这也不起作用 有人愿意启发我吗 示例代码 Public Sub DPArrayStu
  • 使用 python 中的公式函数使从 Excel 中提取的值的百分比相等

    import xlrd numpy excel Users Bob Desktop wb1 xlrd open workbook excel assignment3 xlsx sh1 wb1 sheet by index 0 colA co
  • 如何在Windows的node js中设置默认时区

    我正在尝试在应用程序启动时设置时区 set TZ UTC node main js 但这不起作用 main js var tz process env TZ var date new Date console log tz date UTC
  • 如何使用 C# 了解 Excel 中的分页符 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在使用 C 创建并格式化 Excel 电子表格 因此我需要格式化 合并单元格 更改字体等 直到第一页的最后 如何知道 Excel 电子
  • 使用 php 和 mysql 计算日期差(以小时为单位)

    我如何使用 php 和 mysql 找到以小时为单位的日期差异 Use TIMEDIFF http dev mysql com doc refman 5 1 en date and time functions html function
  • 如何使用Excel的墨迹工具添加手写签名?

    我想在我公司的一些表格中添加手写数字签名 目标是选择一个文档 添加签名 通过使用绘图板 这可以使用 Excel 的墨水工具完成 并将文件作为 PDF 存储在服务器中 这将消除打印然后扫描表格以获得签名的必要性 我使用 Excel 作为文件操
  • 将 Excel 数字日期重新格式化为 R 日期

    希望将从 Excel 中提取的列重新格式化为包含数字 Excel 格式 例如 40182 的数据框 as date 40182 origin 1899 12 30 format b Y Returns 1 2070 年 1 月 5 日 我正
  • 从 CSV 中去除额外的文本限定符

    我有一个 CSV 其中某些字段由 符号分隔作为文本限定符 参见下面的例子 请注意 每个整数 例如 1 2 3 等 都应该是一个字符串 合格的字符串被 符号包围 1 2 3 qualifiedString1 4 5 6 7 8 9 10 11
  • C# ToString("MM/dd/yy") 删除前导 0 [重复]

    这个问题在这里已经有答案了 可能的重复 格式化 NET DateTime Day 不带前导零 https stackoverflow com questions 988353 format net datetime day with no
  • Excel 公式转 SUMIF 日期属于特定月份

    我有以下格式的 Excel 数据 Date Amount 03 Jan 13 430 00 25 Jan 13 96 00 10 Jan 13 440 00 28 Feb 13 72 10 28 Feb 13 72 30 仅当月份位于时 我
  • 绘制持续时间图表

    从我在写这篇文章之前所做的阅读中 我相当确定我需要创建甘特图 但我不知道这是否是正确的路线 需要将开始时间和结束时间的数据作为一个单位绘制在 Excel 图表上 Y 轴为日期 X 轴为一天中的小时 开始时间和结束时间的格式是 Excel 数
  • VBA复制单元格值和格式

    我如何修改以下代码以便不仅复制值而且复制字体样式 例如大胆或不大胆 谢谢 Private Sub CommandButton1 Click Dim i As Integer Dim a As Integer a 15 For i 11 To
  • 如何将 Joda Localdate 转换为 Joda DateTime?

    我试图在执行更多计算之前简单地将时区信息添加回 LocalDate 中 LocalDate 来自使用 ObjectLab LocalDateCalculator 将天数添加到现有 DateTime 但该方法需要返回修改后的 Readable
  • VBA Excel如何在文本框中写入Excel公式

    如何将Excel公式放入文本框中 我尝试过这样的事情 Sub CivBox With ActiveSheet Shapes Civils 3 Copy C26 Activate Paste Shapes Shapes Count Name
  • 将不连续范围从一张纸复制到另一张纸

    VBA 新手 也是第一次发帖 可能会问一个非常基本的问题 然而 我在互联网上 或在我拥有的参考书中 没有找到答案 所以我很困惑 如何将一张纸中的一堆间隔开的列放入另一张纸中 但没有间隙 例如 我想从这样的工作表中复制标记为 x 的单元格 x
  • Azure 网站服务器时区数据错误

    我来自俄罗斯 1 5 天前 10 月 26 日凌晨 2 点 我们的时区 好吧 我们时区的偏移量 发生了变化 从 UTC 4 变为 UTC 3 请不要问为什么现在 政府已经退出了 但它改变了 我家的Win 8 1机器自动更新 但我的 Azur
  • Excel:COUNTIF 函数将“小于”字符视为运算符

    预读说明 我使用的是 LibreOffice 而不是 Excel 但大多数功能应该适用于两者 我正在制作一个电子表格 其中有大量数据 对于每个属性 例如员工数量或姓名 我需要一个函数来计算包含每个不同值的行数 我已经提取了不同的值 现在我使
  • 如何使用 VBA 将行从一张 Excel 工作表复制到另一张 Excel 工作表并创建重复项?

    我有一个包含两张表的 Excel 工作簿 sheet1 在 A 到 R 列中包含一个大型数据表 标题位于第 1 行 Sheet2 在 A 到 AO 列中包含数据 我试图使用 VBA 从sheet1 复制行并将它们粘贴到sheet2 的末尾
  • 如果不是,则必须删除单元格的第一个字符 #3Created 循环永远不会结束

    所以基本上 我需要删除主键字段中第二位数字不为 3 的所有记录 例如可以如下所示 39001 或者没有 3 我想要的是所有以非 3 开头的单元格 它们的行都被删除我想出了以下代码 它删除了所有单元格 但宏永远不会停止运行 Sub keep3
  • PHP date() 和 strtotime() 返回错误的月份 31 日

    我在用着date and strtotime 函数在下拉列表中显示接下来的 3 个月 PHP代码 echo date m Y strtotime 0 months echo date m Y strtotime 1 months echo

随机推荐