C#里操作时间的例子!
2024-07-21 02:18:04
供稿:网友
namespace wrox.samplecode.csharppreview.chbaseclasses
{
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.winforms;
using system.data;
public class formdisplaydatetimes : system.winforms.form
{
... as before
///
/// summary description for formdisplaydatetimes.
/// public formdisplaydatetimes()
{
//
// required for win form designer support
//
initializecomponent();
//
// todo: add any constructor code after initializecomponent call
// datetime dtcurrtime = datetime.now;
additem("current time is " + dtcurrtime.tostring());
additem("year is " + dtcurrtime.year.tostring());
additem("month is " + dtcurrtime.month.tostring());
additem("day of month is " + dtcurrtime.day.tostring());
additem("day of week is " + dtcurrtime.dayofweek.tostring());
additem("hour is " + dtcurrtime.hour.tostring());
additem("minute is " + dtcurrtime.minute.tostring());
additem("second is " + dtcurrtime.second.tostring());
additem("millisecond is " + dtcurrtime.millisecond.tostring());
additem("shortdatestring is " + dtcurrtime.toshortdatestring());
additem("longdatestring is " + dtcurrtime.tolongdatestring());
additem("shorttimestring is " + dtcurrtime.toshorttimestring());
additem("longtimestring is " + dtcurrtime.tolongtimestring());
}
public class formtimespans : system.winforms.form
{
... as before
public formtimespans()
{
//
// required for win form designer support
//
initializecomponent();
//
// todo: add any constructor code after initializecomponent call
//
// constructor: timespan(days, hours, minutes, seconds)
timespan span = new timespan(4,2,15,0);
// initialize date to 1 jan 2000, 12 pm
// constructor: datetime(year,month,day,hours,minutes,seconds,
// milliseconds)
datetime dtold = new datetime(2000,1,1,12,0,0,0);
datetime dtnew = dtold + span;
additem("original date was " + dtold.tolongdatestring() +
" " + dtold.toshorttimestring());
additem("adding time span of " + span.tostring());
additem("result is " + dtnew.tolongdatestring() + " " +
dtnew.toshorttimestring());
additem("");
additem("time span broken down is:");
additem("days: " + span.days.tostring());
additem("hours: " + span.hours.tostring());
additem("minutes: " + span.minutes.tostring());
additem("seconds: " + span.seconds.tostring());
additem("milliseconds: " + span.milliseconds.tostring());
additem("ticks: " + span.ticks.tostring());
additem("");
additem("tickspersecond: " + timespan.tickspersecond.tostring());
additem("ticksperhour: " + timespan.ticksperhour.tostring());
}