Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将系统日期格式转换为C中DateTime可接受的日期格式#_C#_.net_Datetime - Fatal编程技术网

C# 将系统日期格式转换为C中DateTime可接受的日期格式#

C# 将系统日期格式转换为C中DateTime可接受的日期格式#,c#,.net,datetime,C#,.net,Datetime,如何将系统日期格式(如2014年3月18日)转换为DateTime中可读的格式? 我想得到两个日期的总天数,这将来自两个文本框 我尝试过这种语法: DateTime tempDateBorrowed = DateTime.Parse(txtDateBorrowed.Text); DateTime tempReturnDate = DateTime.Parse(txtReturnDate.Text); TimeSpan span = DateTime.Today - tempDateBorrowe

如何将系统日期格式(如2014年3月18日)转换为DateTime中可读的格式? 我想得到两个日期的总天数,这将来自两个文本框

我尝试过这种语法:

DateTime tempDateBorrowed = DateTime.Parse(txtDateBorrowed.Text);
DateTime tempReturnDate = DateTime.Parse(txtReturnDate.Text);
TimeSpan span = DateTime.Today - tempDateBorrowed;
rf.txtDaysBorrowed.Text = span.ToString();
但是
tempdatefollowed
始终返回
DateTime
varible的最小日期。我认为这是因为DateTime没有正确解析我的系统日期格式。因此,它会错误地显示天数。例如,如果我尝试分别输入2014年3月17日和2014年3月18日,我总是得到-365241天,而不是1天

编辑:我希望我的语言环境是非特定的,所以我没有为我的日期格式设置特定的语言环境。(顺便说一下,我的系统格式是en-US)

ToString不是天


您可以尝试在如下文本框中指定日期时间的格式

DateTime tempDateBorrowed = DateTime.ParseExact(txtDateBorrowed.Text.Trim(), "M/d/yyyy", CultureInfo.InvariantCulture);
DateTime tempReturnDate = DateTime.ParseExact(txtReturnDate.Text.Trim(), "M/d/yyyy", CultureInfo.InvariantCulture);

此外,您可能需要检查文本框中的值是否有效。

我的第一个想法是使用
日期时间选择器或等效工具替换
文本框
控件,具体取决于您开发的平台。将字符串转换为日期或将字符串转换为日期比最初看起来更痛苦


或者您可以尝试使用
DateTime.ParseExact
来指定确切的预期格式:

DateTime tempDateBorrowed =
    DateTime.ParseExact("3/17/2014", "M/dd/yyyy", CultureInfo.InvariantCulture);

或者,您可以在对
DateTime.Parse的调用中指定特定的区域性:

var tempDateBorrowed = DateTime.Parse("17/3/2014", new CultureInfo("en-gb"));

var tempDateBorrowed = DateTime.Parse("3/17/2014", new CultureInfo("en-us"));

在使用DateTime.Parse解析日期之前,请尝试将日期格式化为iso 8601或类似的格式


2014-03-17T00:00:00应该与DateTime.Parse一起使用。(“yyyy-MM-ddTHH:MM:ssZ”)

尝试
DateTime.ParseExact
方法

请参阅以下示例代码(自从我使用控制台应用程序编写此代码以来,我一直使用字符串而不是文本框)。希望这有帮助

class Program
    {
        static void Main(string[] args)
        {
            string txtDateBorrowed = "3/17/2014";
            string txtReturnDate = "3/18/2014";
            string txtDaysBorrowed = string.Empty;

            DateTime tempDateBorrowed = DateTime.ParseExact(txtDateBorrowed, "M/d/yyyy", null);
            DateTime tempReturnDate = DateTime.ParseExact(txtReturnDate, "M/d/yyyy", null);
            TimeSpan span = DateTime.Today - tempDateBorrowed;
            txtDaysBorrowed = span.ToString();
        }
    }
试试这个:

if(DateTime.TryParseExact(txtDateBorrowed.Text, "M/d/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDateBorrowed))
{
    TimeSpan span = DateTime.Today - tempDateBorrowed;
}

而且,PARSEACTION方法中的NULL意味着当前的文化,请考虑用当前文化的信息更新您的帖子(类似于“EN US”、“FR FR”、……)在调用解析时取决于它。您也可以从示例中删除不相关的行—只需一行
DateTime.Parse(“12121212”,new CultureInfo(“en-us”)就足够了。