⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datetimeconverter.cs

📁 ajax例子
💻 CS
字号:
//
// Copyright (c) 2005, Michael Schwarz. (http://www.schwarz-interactive.de/)
// All rights reserved.
//   
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met: 
//   
// (1) Redistributions of source code must retain the above copyright 
//     notice, this list of conditions and the following disclaimer.
// (2) Redistributions in binary form must reproduce the above copyright 
//     notice, this list of conditions and the following disclaimer in the
//     documentation and/or other materials provided with the distribution. 
// (3) Neither the name schwarz-interactive nor the names of its contributors
//     may be used to endorse or promote products derived from this software 
//     without specific prior written permission.
//       
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// For some portions of this software, additional copyright notices apply, 
// which can be found in the source code files.
//
// Author: Michael Schwarz, info@schwarz-interactive.de
//
//
using System;

namespace CSharpSample
{
	public sealed class DateTimeConverter : Ajax.JSON.IAjaxObjectConverter
	{
		public void RenderClientScript(ref System.Text.StringBuilder sb)
		{
			sb.Append(@"function digi(v, c){v = v + """";var n = ""0000"";if(v.length < c) return n.substr(0, c-v.length) + v;return v;}
function DateTime(year,month,day,hours,minutes,seconds){if(year>9999||year<1970||month<1||month>12||day<0||day>31||hours<0||hours>23||minutes<0||minutes>59||seconds<0||seconds>59)throw(""ArgumentException"");this.Year = year;this.Month = month;this.Day = day;this.Hours = hours;this.Minutes = minutes;this.Seconds = seconds;}
DateTime.prototype.toString = function(){return digi(this.Year,4) + digi(this.Month,2) + digi(this.Day,2) + digi(this.Hours,2) + digi(this.Minutes,2) + digi(this.Seconds,2);}
");
		}

		public string ClientScriptIdentifier{ get{ return "DateTime"; } }

		public object FromString(string s, Type t)
		{
			if(s == null || s == "")
				return DateTime.Now;


			if(s.Length == 8)
			{
				// TODO: L鋘gen黚erpr黤ung, Monat 1..12, Tag 1..31, Stunde 0..23, Minute 0..59, Sekunde 0..59
				return new DateTime(
					int.Parse(s.Substring(0,4)), 
					int.Parse(s.Substring(4,2)), 
					int.Parse(s.Substring(6,2))
					);
			}
			else if(s.Length == 14)
			{
				// TODO: L鋘gen黚erpr黤ung, Monat 1..12, Tag 1..31, Stunde 0..23, Minute 0..59, Sekunde 0..59
				return new DateTime(
					int.Parse(s.Substring(0,4)), 
					int.Parse(s.Substring(4,2)), 
					int.Parse(s.Substring(6,2)),
					int.Parse(s.Substring(8,2)),
					int.Parse(s.Substring(10,2)),
					int.Parse(s.Substring(12,2))
					);
			}

			throw new NotImplementedException();
		}

		public void ToJSON(ref System.Text.StringBuilder sb, object o)
		{
			if(o.GetType() == typeof(System.DateTime) ||
				o.GetType().BaseType == typeof(System.DateTime))
			{
				DateTime d = (DateTime)o;
				sb.Append("new Date(" + d.Year + "," + (d.Month -1) + "," + d.Day + "," + d.Hour + "," + d.Minute + "," + d.Second + ")");
			}
		}

		public Type[] SupportedTypes
		{
			get
			{
				return new Type[]{typeof(System.DateTime)};
			}
		}

		public bool IncludeSubclasses
		{
			get{ return true; }
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -