日期格式问题

问题描述:

如果我调用此方法的顺序性,日期格式问题

object[] ab = GetSomething(myObject); 

我得到的日期时间格式是这样,这是确定

enter image description here

知道如果我使用第三方物流,调用该方法

Task t1 = Task.Factory.StartNew(() => GetSomething(myObject)); 
Task t2 = Task.Factory.StartNew(() => GetSomeOtherthing(myObject)); 
Task.WaitAll(t1, t2); 

我与AM/PM这种格式是causi ng转换失败,说无效的日期时间格式,有没有办法像顺序方法改变日期时间formart。

enter image description here

我是多么转换字符串日期时间

Search.Date = Convert.ToDateTime(myObject.ToDate, CultureInfo.InvariantCulture); 
+2

什么是'被设置'Date' GetSomething'的代码? – 2012-02-14 17:10:28

+0

@NeilKnight使用Convert.ToDateTime(myObject.ToDate)的iam采用字符串格式 – 2012-02-14 17:15:05

+0

接下来的问题是'myObject'上的'ToDate'是什么?属性?它是如何实现的?它的数据类型是什么? – 2012-02-14 17:17:41

从/转换为字符串时,应始终明确指定的文化。

在你的情况下,线程池线程可能与你所期望的不同CurrentCulture。

+0

有没有办法将线程池线程CurrentCulture更改为特定的线程池线程。 – 2012-02-14 17:59:08

+3

@ AI25我会避免这种情况。 'DateTime.Parse'和类似的函数有一个参数,需要一种文化。使用那个。 – CodesInChaos 2012-02-14 18:02:11

+0

如果您使用不使用特定CultureInfo而使用DateTime.Pars/DateTime.ToString的库,那么该怎么办......我认为您需要在Thread上设置CurrentCulture。 – Jaap 2012-04-26 09:51:04

如果您想更改线程的文化,然后创建自己的知道应用程序文化的任务计划程序。调度程序可以在执行任务之前调整文化。

这是一个样本调度...

class LocalizedTaskScheduler : TaskScheduler 
{ 
    public CultureInfo Culture { get; set; } 
    public CultureInfo UICulture { get; set; } 

    #region Overrides of TaskScheduler 

    protected override void QueueTask(Task task) 
    { 
     //Queue the task in the thread pool 
     ThreadPool.UnsafeQueueUserWorkItem(_ => 
     { 
      //Adjust the thread culture 
      Thread.CurrentThread.CurrentCulture = this.Culture; 
      Thread.CurrentThread.CurrentUICulture = this.UICulture; 
      //Execute the task 
      TryExecuteTask(task); 
     }, null); 
    } 

    protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) 
    { 
     if (taskWasPreviouslyQueued) 
     { 
      return false; 
     } 
     // Try to run the task. 
     return base.TryExecuteTask(task); 
    } 

    protected override IEnumerable<Task> GetScheduledTasks() 
    { 
     //We have no queue 
     return Enumerable.Empty<Task>(); 
    } 

    #endregion 
}