读书人

C#选择日期和时间的控件解决方法

发布时间: 2012-04-20 15:27:03 作者: rapoo

C#选择日期和时间的控件
在C#中有没有这样的日期时间控件:不仅日期可以自由选择,也可以自由选择时、分、秒;我用DateTimePicker控件不能选择时、分、秒,请问应该用什么方法实现?先谢谢了!

[解决办法]

JScript code
var cal;var isFocus=false; //是否为焦点/*选择日期 → 由 寒羽枫 2006-06-25 添加*/function SelectDate(obj,strFormat){    var date = new Date();    var by = date.getFullYear()-5;  //最小值 → 50 年前    var ey = date.getFullYear();  //最大值 → 50 年后    cal = (cal==null) ? new Calendar(by, ey, 0) : cal;    //不用每次都初始化 2006-12-03 修正    cal.dateFormatStyle = strFormat;    cal.show(obj);}/* * 返回日期 * @param d the delimiter * @param p the pattern of your date 2006-06-25 由 寒羽枫 修改为根据用户指定的 style 来确定; */String.prototype.toDate = function(style) {  var y = this.substring(style.indexOf('y'),style.lastIndexOf('y')+1);//年  var m = this.substring(style.indexOf('M'),style.lastIndexOf('M')+1);//月  var d = this.substring(style.indexOf('d'),style.lastIndexOf('d')+1);//日  if(isNaN(y)) y = new Date().getFullYear();  if(isNaN(m)) m = new Date().getMonth();  if(isNaN(d)) d = new Date().getDate();  var dt ;  eval ("dt = new Date('"+ y+"', '"+(m-1)+"','"+ d +"')");  return dt;}/* * 格式化日期 * @param   d the delimiter * @param   p the pattern of your date * @author  meizz */Date.prototype.format = function(style) {  var o = {    "M+" : this.getMonth() + 1, //month    "d+" : this.getDate(),      //day    "h+" : this.getHours(),     //hour    "m+" : this.getMinutes(),   //minute    "s+" : this.getSeconds(),   //second    "w+" : "天一二三四五六".charAt(this.getDay()),   //week    "q+" : Math.floor((this.getMonth() + 3) / 3),  //quarter    "S"  : this.getMilliseconds() //millisecond  }  if(/(y+)/.test(style)) {    style = style.replace(RegExp.$1,    (this.getFullYear() + "").substr(4 - RegExp.$1.length));  }  for(var k in o){    if(new RegExp("("+ k +")").test(style)){      style = style.replace(RegExp.$1,        RegExp.$1.length == 1 ? o[k] :        ("00" + o[k]).substr(("" + o[k]).length));    }  }  return style;}/* * 日历类 * @param   beginYear 1990 * @param   endYear   2010 * @param   lang      0(中文)|1(英语) 可自由扩充 * @param   dateFormatStyle  "yyyy-MM-dd"; * @version 2006-04-01 * @author  KimSoft (jinqinghua [at] gmail.com) * @update */function Calendar(beginYear, endYear, lang, dateFormatStyle) {  this.beginYear = 1990;  this.endYear = 2010;  this.lang = 0;            //0(中文) | 1(英文)  this.dateFormatStyle = "yyyy-MM-dd";  if (beginYear != null && endYear != null){    this.beginYear = beginYear;    this.endYear = endYear;  }  if (lang != null){    this.lang = lang  }  if (dateFormatStyle != null){    this.dateFormatStyle = dateFormatStyle  }  this.dateControl = null;  this.panel = this.getElementById("calendarPanel");  this.container = this.getElementById("ContainerPanel");  this.form  = null;  this.date = new Date();  this.year = this.date.getFullYear();  this.month = this.date.getMonth();  this.colors = {  "cur_word"      : "#FFFFFF",  //当日日期文字颜色  "cur_bg"        : "#00FF00",  //当日日期单元格背影色  "sel_bg"        : "#FFCCCC",  //已被选择的日期单元格背影色 2006-12-03 寒羽枫添加  "sun_word"      : "#FF0000",  //星期天文字颜色  "sat_word"      : "#0000FF",  //星期六文字颜色  "td_word_light" : "#333333",  //单元格文字颜色  "td_word_dark"  : "#CCCCCC",  //单元格文字暗色  "td_bg_out"     : "#EFEFEF",  //单元格背影色  "td_bg_over"    : "#FFCC00",  //单元格背影色  "tr_word"       : "#FFFFFF",  //日历头文字颜色  "tr_bg"         : "#666666",  //日历头背影色  "input_border"  : "#CCCCCC",  //input控件的边框颜色  "input_bg"      : "#EFEFEF"   //input控件的背影色  }  this.draw();  this.bindYear();  this.bindMonth();  this.changeSelect();  this.bindData();}/* * 日历类属性(语言包,可自由扩展) */Calendar.language = {  "year"   : [[""], [""]],  "months" : [["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],        ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]         ],  "weeks"  : [["日","一","二","三","四","五","六"],        ["SUN","MON","TUR","WED","THU","FRI","SAT"]         ],  "clear"  : [["清空"], ["CLS"]],  "today"  : [["今天"], ["TODAY"]],  "close"  : [["关闭"], ["CLOSE"]]} 


[解决办法]
winform
DateTimePicker
设置属性
Format=Custom
CustomFormat=yyyy-mm-dd HH:mm:ss
[解决办法]
CustomFormat=yyyy-MM-dd HH:mm:ss
[解决办法]
是的将format属性设置为custom
然后customformat=yyyy-MM-dd (ddd) HH:mm:ss
还可以显示星期几!


[解决办法]

探讨
大家不要理解错了,我现在用DateTimePicker能显示时间,但是我想选择时间,自由选择某一个时间点

[解决办法]
教你个最简单的方法 DataTimePaker1.Format=custom DataTimePaker1.CustomeFormat= yyyy-MM-dd HH:mm:ss就行了

读书人网 >C#

热点推荐