显示当前时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了显示当前时间相关的知识,希望对你有一定的参考价值。
利用VC++的WM_TIMER消息编写一个基于对话框的Win32程序,完成每秒在屏幕上显示当前时间的功能,获取当前时间可用MFC的CTime类
求程序,最好附有讲解
COleDateTime dtCurrent; //用于获取系统时间
dtCurrent = COleDateTime::GetCurrentTime(); //获取系统当前时间
CString strDT = dtCurrent.Format("%Y-%m-%d %H:%M:%S"); //格式化时间
//下面可增加对时间的其他处理 如输出到控件...等等
//.....
//下面设置定时器
每秒产生一个消息即可....(略) 参考技术A 以下是头文件
#include <time.h>
#include <dos.h>
#include <iostream>
using namespace std;
class Time
public:
Time();
void SetTime(int hr,int min,int sec);
void Tick();
GetTime();
private:
int hour;
int minute;
int second;
;
以下是实现
#include "date.h"
Date::Date(int mn,int dy,int yt)
month=(mn>0&&mn<=12)?mn:1;
year=yt;
day=CheckDay(dy);
cout<<"Date object constructor for date\n";
Print();
void Date::Print() const
cout<<"the date in the object is:"
<<month<<"-"<<day<<"-"<<year<<endl;
int Date::CheckDay(int testDay)
static int DaysPerMonth[13]=0,31,28,31,30,
31,30,31,30,
31,30,31;
if(month!=2)
if(testDay>0&&testDay<=DaysPerMonth[month])
return testDay;
else
return 1;
else
int days=(year%400==0
||(year%4==0&&year%100!=0))?29:28;
if(testDay>0&&testDay<=days)
return testDay;
else
return 1;
void Date::NextDay()
static int DaysPerMonth[13]=0,31,28,31,30,
31,30,31,31,30,
31,30,31;
int days=DaysPerMonth[month];
if((day+1)%(days+1)==0)
day=1;
else
day=day+1;
if(day==1)
if((month+1)%13==0)
month=1;
year=year+1;
else
month=month+1;
Print();
int main()
Date testd(1,1,2003);
for(int i=1;i<300;i++)
testd.NextDay();
return 0;
#include "creatTime.h"
Time::Time()
struct tm *pt;
time_t t;
t=time(NULL);
pt=localtime(&t);
//cout<<pt;
hour=pt->tm_hour;
minute=pt->tm_min;
second=pt->tm_sec;
cout<<"the time is:->"<<hour
<<":"<<minute<<":"<<second<<endl;
void Time::SetTime(int hr,int min,int sec)
hour=(hr>=0&&hr<24)?hr:0;
minute=(min>=0&&min<60)?min:0;
second=(sec>=0&&sec<60)?sec:0;
cout<<"the time is:->"<<hour
<<":"<<minute<<":"<<second<<endl;
void Time::Tick()
second=(second+1)%60;
if(second==0)
minute=(minute+1)%60;
if(minute==0)
hour=(hour+1)%24;
else
hour=hour;
else
minute=minute;
cout<<"the time is:->"<<hour
<<":"<<minute<<":"<<second<<endl;
int main()
Time loctime;
loctime.SetTime(20,30,40);
Time t;
t.SetTime(55,20,77);
t.Tick();
return 0;
下边是关于日期是头文件
//#ifdef DATE_H
//#define DATE_H
#include<iostream>
using namespace std;
class Date
public:
Date(int=1,int=1,int=1990);
void NextDay();
void Print() const;
private:
int month;
int day;
int year;
int CheckDay(int);
;
//#endif
页面动态显示当前时间
$(document).ready(function(){ display(); }) function display() { var time = new Date(); //获得当前时间 var week = "日一二三四五六".charAt(time.getDay()); var year = time.getYear() + 1900; var month = time.getMonth() + 1;
//获得小时、分钟、秒 var hour = time.getHours(); var minute = time.getMinutes(); var second = time.getSeconds(); if(minute < 10){ minute="0"+minute;} if(second < 10){ second = "0"+second; } var innerHTML = year + "年" + month + "月" + time.getDate() + "日 " + hour + ":" + minute + ":" + second + " " + "星期" + week; $("#nowTime").html(innerHTML); setTimeout("display()", 1000); }
以上是关于显示当前时间的主要内容,如果未能解决你的问题,请参考以下文章