2009-06-01 91 views
0

link text.Net:DateRange无法转换为类型'日期

我遇到了同样的问题。我的日期格式是MM/dd/yyyy。如何在我的系统中找到日期格式并用代码进行更改。

+1

你真的应该包括在这个网站上的问题的文本,因为你的链接的线程可能会消失,然后这个线程将是无用的,因为在将来如果有人读它,他们会不知道你在说什么指的是。 – Kelsey 2009-06-01 21:48:39

回答

0

您应该为current thread设置适当的文化。 DateTime.Parse从当前线程(顺便说一句,I love you, Reflector!)的培养使用日期设置:

public static DateTime Parse(string s) 
{ 
    return DateTimeParse.Parse(s, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None); 
} 

其中 “DateTimeFormatInfo.CurrentInfo” 是:

public static DateTimeFormatInfo CurrentInfo 
{ 
    get 
    { 
     CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture; 
     if (!currentCulture.m_isInherited) 
     { 
      DateTimeFormatInfo dateTimeInfo = currentCulture.dateTimeInfo; 
      if (dateTimeInfo != null) 
      { 
       return dateTimeInfo; 
      } 
     } 
     return (DateTimeFormatInfo) currentCulture.GetFormat(typeof(DateTimeFormatInfo)); 
    } 
} 

有很多如何对全球化,只是尝试搜索它们(example of such how-to :))

0

如果它是一个日期对象,那么你只需要使用ToString方法来格式化它。

http://msdn.microsoft.com/en-us/library/system.datetime.tostring.aspx

代码示例:

using System; 

public class DateToStringExample 
{ 
    public static void Main() 
    { 
     DateTime dateValue = new DateTime(2008, 6, 15, 21, 15, 07); 
     // Create an array of standard format strings. 
     string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o", 
           "R", "s", "t", "T", "u", "U", "y"}; 
     // Output date and time using each standard format string. 
     foreach (string standardFmt in standardFmts) 
     Console.WriteLine("{0}: {1}", standardFmt, 
          dateValue.ToString(standardFmt)); 
     Console.WriteLine(); 

     // Create an array of some custom format strings. 
     string[] customFmts = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f", 
          "dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" }; 
     // Output date and time using each custom format string. 
     foreach (string customFmt in customFmts) 
     Console.WriteLine("'{0}': {1}", customFmt, 
          dateValue.ToString(customFmt)); 
    } 
} 
// This example displays the following output to the console: 
//  d: 6/15/2008 
//  D: Sunday, June 15, 2008 
//  f: Sunday, June 15, 2008 9:15 PM 
//  F: Sunday, June 15, 2008 9:15:07 PM 
//  g: 6/15/2008 9:15 PM 
//  G: 6/15/2008 9:15:07 PM 
//  m: June 15 
//  o: 2008-06-15T21:15:07.0000000 
//  R: Sun, 15 Jun 2008 21:15:07 GMT 
//  s: 2008-06-15T21:15:07 
//  t: 9:15 PM 
//  T: 9:15:07 PM 
//  u: 2008-06-15 21:15:07Z 
//  U: Monday, June 16, 2008 4:15:07 AM 
//  y: June, 2008 
//  
//  'h:mm:ss.ff t': 9:15:07.00 P 
//  'd MMM yyyy': 15 Jun 2008 
//  'HH:mm:ss.f': 21:15:07.0 
//  'dd MMM HH:mm:ss': 15 Jun 21:15:07 
//  '\Mon\t\h\: M': Month: 6 
//  'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00 
0

你的问题是,你分配在aspx文件的日期和期望的格式是MM/DD/YYYY时,它可能是DD/MM/yyyy所以它吹了。您是否正在部署到使用不同日期格式的系统?

如果是这样,也许手动通过代码添加范围,并使用期望日期对象的预期格式分配它们。

例如,

YourDateValidator.MaximumValue = YourMaxDateTimeObject; 
YourDateValidator.MinimumValue = YourMinDateTimeObject; 
+0

@Jack如果这个答案或另一个帮助你,你应该设置一个被接受的答案,以便其他人阅读这个问题可以找到帮助你的解决方案。 – Kelsey 2010-08-18 16:03:39

相关问题