VB – 不同机器上字符串日期连接导致不同结果

huangapple go评论72阅读模式
英文:

VB - Different Results With String-Date Concatenation On Different Machines

问题

我的同事和我在调试一个问题时遇到了一个奇怪的情况:相同的VB代码在我的机器和他的机器上产生了不同的输出。我们无法弄清楚为什么。

这段代码是这样的:

Dim textForMatching = unmatchedRecord.MetaDataString + " - " + unmatchedEvent.StartDate?.ToLocalTime().Date

在我的机器上,输出结果可能是类似于 "10d775fff794 - 06/15/2023",而在他的机器上,输出结果是 "10d775fff794 - 6/15/2023"。我的日期输出中那个前导的 "0" 在他的机器上丢失了。

我们处于相同的时区,使用的是相同版本的Visual Studio。

有人知道为什么会发生这种情况吗?这是由于使用 "+" 运算符进行隐式转换为字符串导致的本地化相互作用吗?还是我们的计算机上可能有稍微不同的设置导致了这个问题?

英文:

My co-worker and I were debugging an issue when we came across something curious: the same bit of VB code produces a different output on my machine versus his. We cannot figure out why.

The code:

Dim textForMatching = unmatchedRecord.MetaDataString + " - " + unmatchedEvent.StartDate?.ToLocalTime().Date

On my machine, the output would be something like 10d775fff794 - 06/15/2023 while on his machine the output is 10d775fff794 - 6/15/2023. That leading 0 in my date output is missing from his.

We're in the same time-zone and we're both using the same version of Visual Studio.

Does anyone know why this happens? It is some localization interplay due to the implicit conversion to a string by using the + operator? Or are there settings on our computers which could be slightly different and lead to this?

答案1

得分: 2

检查您的注册表:HKEY_CURRENT_USER\Control Panel\International,查看sShortDate设置。它在每台机器上是否不同?

我相信这是确定要使用的日期格式的方式。

英文:

Check your registry: HKEY_CURRENT_USER\Control Panel\International the sShortDate setting. Is it different on each machine?

I believe this is how it determines the date format to use.

答案2

得分: 1

你应该学习文档

然后你会发现:

> 请注意,确切的输出取决于当前的区域设置和系统上运行的本地时区。

所以,原因是你正在使用不同的Windows日期和时间设置。

英文:

You should study the documentation.

Then you'll find:

> Note that the exact output depends on the current culture and the
> local time zone of the system on which it is run.

So, the reason is that you are using different Windows settings for date and time.

huangapple
  • 本文由 发表于 2023年6月16日 03:57:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定