简体   繁体   English

日期时间从 vb.net 到 C#

[英]DateTime from vb.net to C#

So I'm trying to convert a code from .NET to C# Here's the code:所以我试图将代码从 .NET 转换为 C# 这是代码:

While getReader.Read

        ' getting the time in and time out 12 hour format
        time_in = (getReader.Item("time_in").ToString)
        time_out = (getReader.Item("time_out").ToString)

        With Listview1.Items.Add(getReader.Item("worker_fullname").ToString)

            .SubItems.Add(Format(time_in, "h:mm:ss tt"))
            .SubItems.Add(Format(time_out, "h:mm:ss tt"))
            .SubItems.Add(Format(getReader.Item("date"), "MMM dd, yyyy"))

        End With

    End While

Online code conversion does not help me at all.在线代码转换对我没有任何帮助。 I'm having difficulty in (getReader.Item("time_in").ToString) .我在(getReader.Item("time_in").ToString)中遇到困难。 I tried DateTime.TryParseExact(getReader[3], "h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out time_in);我试过DateTime.TryParseExact(getReader[3], "h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out time_in); but it says that it cannot convert from object to string.但它说它不能从对象转换为字符串。 And in the End and End With , it seems that C# doesn't have an equivalent language construct for that.EndEnd With ,C# 似乎没有对应的语言结构。

调用ToString()

DateTime.TryParseExact(getReader[3].ToString(), "h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out time_in);
while (reader.Read())
{
    var timeIn = reader["time_in"].ToString();
    var timeOut = reader["time_out"].ToString();
    var workerFullName = reader["worker_fullname"].ToString();
    var date = reader["date"].ToString();

    //move to utility method 
    DateTime timeInDateTime;
    DateTime.TryParseExact(timeIn, "h:mm:ss tt", null, DateTimeStyles.None, out timeInDateTime);

    DateTime timeOutDateTime;
    DateTime.TryParseExact(timeOut, "h:mm:ss tt", null, DateTimeStyles.None, out timeOutDateTime);

    DateTime dateParsed;
    DateTime.TryParseExact(date, "MMM dd, yyyy", null, DateTimeStyles.None, out dateParsed);

    //add to list
}

WITH ... END WITH does not have equivalent in C# (regardless of how much I'd like it). WITH ... END WITH 在 C# 中没有等价物(不管我有多喜欢它)。 One has to assign it to a local variable such as:必须将其分配给局部变量,例如:

...
var item = Listview1.Items.Add(getReader.Item("worker_fullname").ToString);
item.SubItems.Add($"{time_in, "h:mm:ss tt"))
...

As for getting data from reader it depends on how are those data stored in storage (database).至于从阅读器获取数据,这取决于这些数据如何存储在存储(数据库)中。 In your case i guessc either getReader.GetString(3) or getReader.GetDateTime(3)在你的情况下,我猜是 getReader.GetString(3) 或 getReader.GetDateTime(3)

尝试这个

(getReader.Item("time_in").Value.ToString)

you can use this你可以用这个

DateTime dt = DateTime.parse(reader["time_in"].toString("MM/dd/yyyy hh:mm:ss tt")); DateTime dt = DateTime.parse(reader["time_in"].toString("MM/dd/yyyy hh:mm:ss tt"));

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

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