【问题标题】:DateTime.Compare in c#日期时间。比较在 c# 中
【发布时间】:2017-01-30 21:50:12
【问题描述】:

我目前正在尝试将我的 vb.net 代码的一部分转换为 c#,但我似乎无法获得正确的语法。

我的 vb.net 代码是

Private Sub board(days As Integer, name As String)

Dim dcount As Integer = 0
Dim counter As Integer = 0


    If My.Computer.FileSystem.FileExists("pathway" + name) AndAlso Not File.ReadAllText("pathway" + name).Length = 0 Then

        Dim d As List(Of String) = File.ReadAllLines("pathway" + name).ToList
        Dim line As String = d(0)

        While counter <> d.Count
            line = d(counter)


            If DateTime.Compare(line.Substring(0, line.LastIndexOf(",")), Now.AddDays(days).ToString("MM/dd/yyyy")) < 0 Then
                dcount += 0
                counter += 1
            Else
                dcount += 1
                counter += 1

            End If
        End While
    End If

vb.net 代码运行良好,但我的 c# 下面给出了错误:

运算符'

错误所在的行是:

if (DateTime.Compare(line.Substring(0, line.LastIndexOf(",")), DateTime.Now.AddDays(days).ToString("MM/dd/yyyy") < 0)) {

整个部分都在下面

  private void board(int days, string name){


 int dcount = 0;
        int counter = 0;

if (File.Exists(@"pathway" + name) && File.ReadAllText(@"pathway" + name).Length != 0)
        {
            List<string> d = File.ReadAllLines(@"pathway" + name).ToList();
            string line = d[0];

            while (counter != d.Count)
            {
                line = d[counter];
                // compares the current date to the amount of days you put in the days integer
                if (DateTime.Compare(line.Substring(0, line.LastIndexOf(","), DateTime.Now.AddDays(days).ToString("MM/dd/yyyy") < 0) {
                    counter++;
                } else
                {
                    dcount++;
                    counter++;
                }
            }
        } 
 }

感谢你们能给我的任何帮助

【问题讨论】:

  • 您提供的代码甚至在语法上都不正确。有错误的行至少缺少一个右括号...我猜在line.LastIndexOf(",")之后
  • 请提供line的示例。
  • 试试这个` if(DateTime.Compare(DateTime.Parse(line.Substring(0, line.LastIndexOf(","))), DateTime.Now.AddDays(days))
  • 有一个免费的 Telerik 在线工具,您可以使用它来将 VB 转换为 C#,您唯一需要做的就是在 C# 代码中将索引周围的 ( ) 更改为 [ ],非常简单

标签: c# vb.net operator-keyword


【解决方案1】:

DateTime.Compare 只能用于比较DateTime 对象。

在 VB.NET 中,存在隐式类型转换,但在 c# 中,您必须是显式的。

代替

if (DateTime.Compare(line.Substring(0, line.LastIndexOf(",")), DateTime.Now.AddDays(days).ToString("MM/dd/yyyy") < 0)) 
{
    //Do something
}

你需要写一些类似的东西

var d1 = DateTime.Parse(line.Substring(0, line.LastIndexOf(","));
var d2 = DateTime.Now.AddDays(days);
if (DateTime.Compare(d1, d2) < 0) 
{
    //Do something
}

如果您真的希望将所有内容集中在一行上,您可以这样做,但可能有点难以阅读:

if (DateTime.Compare(DateTime.Parse(line.Substring(0, line.LastIndexOf(",")), DateTime.Now.AddDays(days) < 0) 
{
    //Do something
}

【讨论】:

  • 好答案。喜欢补充:为什么原始海报使用DateTime.Compare?小于 ("
  • 感谢您的回答! @ChiralMichael 是什么意思?
  • 我的意思是,使用if ( d1 &lt; d2 ) { ...。它相当于if (DateTime.Compare( d1, d2 ) &lt; 0) { ...。我努力只使用比较方法,除非(1)我有一个自定义比较器,(2)我有一个指定的比较类型,如StringComparison.OrdinalIgnoreCase 或(3)比较运算符未在该类型上定义。这些都不适用于这里。此外,我非常有动力,因为我发现&lt; 更清晰。
猜你喜欢
  • 2012-01-20
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-20
相关资源
最近更新 更多