2009-06-01 19 views
0

link text.Net:DateRangeを型に変換できません。日付

私は同じ問題があります。私の日付形式はMM/dd/yyyyです。私のシステムで日付形式を調べ、コードでそれを変更するにはどうすればいいですか?

+1

リンク先のスレッドが消える可能性があるので、実際にこのサイトの質問のテキストを含める必要があります。あなたが何を知っていない〜を指していた。 – Kelsey

答えて

0

current threadの適切な培養を設定する必要があります。 DateTime.Parseは、現在のスレッド(ところで、I love you, Reflector!)の培養物から日付の設定を使用しています: "DateTimeFormatInfo.CurrentInfo" は

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

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

Dateオブジェクトの場合は、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ファイルの日付を割り当て、それはおそらく、DD/mmであるとき、YYYY/MM/DDをする形式を期待していることですyyyyだから爆発している。別の日付形式を使用するシステムに展開していますか?

もしそうなら、手動で範囲をコードに追加し、日付オブジェクトが必要な予想される形式を使用して範囲を割り当てることができます。

例:

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

@Jackこの回答が役立つ場合、この質問を読んでいる他の人があなたを助けた解決策を見つけることができるように、受け入れられた回答を設定する必要があります。 – Kelsey

関連する問題