VB。NET 将日期转换为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25010823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:54:59  来源:igfitidea点击:

Vb. NET Convert a date to number

vb.net

提问by Gherghina Ionut-Valentin

I have this code in VB . NET

我在 VB 中有这段代码。网

dim date_e As DateTime
date_e = New DateTime(CLng(Convert.ToDouble("635434240520170000")))

The result is:

结果是:

12.08.2014 07:07:32

Now my question is how i can reverse that encoding to obtain the number from a specific date and time witch i input myself: Lets' say.

现在我的问题是如何反转该编码以从我自己输入的特定日期和时间获取数字:让我们说。

22.09.2014 07:07:32

Thank you!

谢谢!

回答by Tim Schmelter

The DateTimeconstructorthat takes a Longare the ticks since January 1, 0001 at 00:00:00.000 in the Gregorian calendar.

采用 a的DateTime构造函数Long自公历 0001 年 1 月 1 日 00:00:00.000 以来的刻度。

You just need to parse the string to Datefirst, then you can use it's Ticksproperty:

您只需Date要先解析字符串,然后就可以使用它的Ticks属性

Dim dt = Date.Parse("22.09.2014 07:07:32") ' presumes that this is the correct format
Dim ticks As Long = dt.Ticks

If the input date-string is in a different format than your current culture you can use Date.Parsewith the correct culture:

如果输入日期字符串的格式与您当前的文化不同,您可以使用Date.Parse正确的文化:

dt = Date.Parse("22.09.2014 07:07:32", New CultureInfo("de-DE"))

or - if you don't know the culture but only the format - Date.ParseExact:

或者-如果您不了解文化而只了解格式- Date.ParseExact

dt = Date.ParseExact("22.09.2014 07:07:32", "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture)