Convert.ToDateTime在C#中从特定日期字符串 [英] Convert.ToDateTime In C# From Specific Date String

查看:1374
本文介绍了Convert.ToDateTime在C#中从特定日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具体的日期字符串13/02 / 07,16:05:13 + 00

I have a specific date string: "13/02/07,16:05:13+00"

我想将其转换在 C#日期时间 Convert.toDateTime ),但我不断收到错误。

I am trying to convert it in C# to a datetime (Convert.toDateTime) but I keep receiving errors.

我也尝试过使用下面的code无果来分析吧:

I have also tried to parse it using the following code to no avail:

 DateTime dt = DateTime.ParseExact(date, "yy/MM/dd,hh:mm:ss+00",System.Globalization.CultureInfo.CurrentCulture);

我要如何转换字符串日期时间一个真正具体的日期时间 字符串

How do I convert a string to datetime for a really specific datetime string.

推荐答案

不要使用的CurrentCulture - 使用固定区域性。否则,你会拿起当前区域性的日期和时间分隔符。

Don't use CurrentCulture - use the invariant culture. Otherwise you'll be picking up the current culture's date and time separators.

此外,你需要使用 HH 而不是 HH 如您使用的是24小时制,不是一个12小时制。这工作得很好:

Also, you need to use HH instead of hh as you're using a 24-hour clock, not a 12-hour clock. This works fine:

using System;
using System.Collections.Generic;
using System.Globalization;

class Test
{
    static void Main()
    {
        string date = "13/02/07,16:05:13+00";
        DateTime dt = DateTime.ParseExact(date, "yy/MM/dd,HH:mm:ss+00",
                                          CultureInfo.InvariantCulture);
        Console.WriteLine(dt);
    }
}

时的+00肯定总是会一样吗?如果你需要应付非零点偏置,它会改变的事情一点。

Is the "+00" definitely always going to be the same? If you need to cope with non-zero offsets, it will change things a bit.

这篇关于Convert.ToDateTime在C#中从特定日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆