English 中文(简体)
2. 如何转换地体
原标题:How to convert string to date time
 string value =     "Sat Apr 28 2012 11:00:00 GMT-0400 (Eastern Daylight Time)"

我需要随时转换。

我试图这样做:

 DateTime datetime = DateTime.ParseExact(value, "MM/dd/yyyy hh:mm", null);

 DateTime datetime2 = Convert.ToDateTime(value);

例外:严格限制不被视为有效日期。

最佳回答
问题回答

您重述了“MM/dd/yyyy hh:mm”的格式,但你以这种格式的扼制灯塔甚至slightly

我怀疑你对“GMT-0400(东日光时间)”部分有异议,其余部分以“dd MMM dd yyyyy HH:mm:s”或“ddd MMM d yyyyy HH:mm:s”为形式,如果月数isn t总是有两位数的话。

我建议你分别从美洲金枪鱼委中拨款,并创建<条码>。 日期:Offset - parse the first part ( before GMT) as an unspecisedDatetime - 然后将抵消额平整。 EDIT:你可以将冲抵打<代码>。 时间Span.ParseExact,但你需要处理标志,我认为,我看不出任何有文件记载的贬低逆时段的方法:

EDIT: Note that my LocalDatetime部分,但你仍需要将探测的不同部分相互分开。 样本代码:

using System;
using System.Linq;
using System.Xml.Linq;
using NodaTime;
using NodaTime.Text;

public class Test
{
    static void Main()
    {
        string text = "Sat Apr 28 2012 11:00:00 GMT-0400 (Eastern Daylight Time)";
        ZonedDateTime parsed = Parse(text);
        Console.WriteLine(parsed);
    }

    static readonly LocalDateTimePattern LocalPattern =
        LocalDateTimePattern.CreateWithInvariantInfo("ddd MMM d yyyy HH:mm:ss");

    // Note: Includes space before GMT for convenience later
    static readonly OffsetPattern OffsetPattern =
        OffsetPattern.CreateWithInvariantInfo("  GMT +HHmm");

    static ZonedDateTime Parse(string text)
    {
        int gmtIndex = text.IndexOf(" GMT");
        int zoneIndex = text.IndexOf(" (");
        // TODO: Validation that these aren t -1 :)

        string localText = text.Substring(0, gmtIndex);
        string offsetText = text.Substring(gmtIndex, zoneIndex - gmtIndex);

        var localResult = LocalPattern.Parse(localText);
        var offsetResult = OffsetPattern.Parse(offsetText);

        // TODO: Validate that both are successful

        var fixedZone = DateTimeZone.ForOffset(offsetResult.Value);        
        return localResult.Value.InZoneStrictly(fixedZone);
    }
}

请注意,这将在固定上提供<代码><日<>>。 时间区——实际上不是东部时间。 A. 目前 时间t有<条码>OffsetDatetime,此条自然适用。

如下:

Convert.ToDateTime("Sat Apr 28 2012 11:00:00 GMT-0400 (Eastern Daylight Time)".Substring(4, 20))




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签