显示当前时间

这里使用了localtime_s来获取

代码如下:

// c++_d.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <ctime>

using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
	errno_t err;
	tm pt = {0};
	time_t t;
	time(&t);//获取当前时间
	err = localtime_s(&pt, &t);
	cout<< pt.tm_year + 1900 <<"-"<< pt.tm_mon + 1<< "-"<< pt.tm_mday<<"  "
		<<pt.tm_hour<<" "<< pt.tm_min<<" "<<pt.tm_sec<<endl;
	
	system("pause");
	return 0;
}


ATL::CTime和ATL::CTimeSpan

代码如下:

// c++_e.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <atltime.h>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int year, month, day;
	cout<<"输入你的出生日期:"<<endl;
	cin >> year >> month >> day;

	if (year && month && day)
	{
		CTime birthday(year, month, day, 0, 0, 0);
		CTime now = CTime::GetCurrentTime();

		CTimeSpan span = now - birthday;

		cout<<"你已经生活在地球上时间为: " <<span.GetTotalHours()<<" 小时"<<endl;

	}

	system("pause");

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是使用Python的turtle库显示当前时间的示例代码: ```python import turtle import datetime # 创建turtle对象 t = turtle.Turtle() # 获取当前时间 now = datetime.datetime.now() # 将时间格式化为字符串 time_str = now.strftime("%H:%M:%S") # 设置画笔颜色和字体大小 t.color("red") t.pensize(5) t.write(time_str, font=("Arial", 30, "bold")) # 隐藏turtle箭头 t.hideturtle() # 等待窗口关闭 turtle.done() ``` 运行上述代码,将会在窗口中显示当前时间。注意,这里使用的是24小时制。如果你想使用12小时制,可以将"%H"替换为"%I",并添加"%p"来显示AM或PM,例如: ```python time_str = now.strftime("%I:%M:%S %p") ``` ### 回答2: 为了使海龟(turtle)显示当前时间,我们可以使用Python的内置模块datetime来获取当前日期和时间。首先,我们需要导入datetime模块。然后,通过使用datetime模块中的datetime类的now()方法,我们可以获取当前的日期和时间。接着,我们可以使用turtle模块来创建一个海龟对象,并使用其相关方法来显示日期和时间。例如,我们可以使用turtle的write()方法来向屏幕上写入日期和时间的字符串表示形式。最后,我们使用turtle的done()方法来保持显示的窗口打开状态,以便我们可以看到当前时间显示。 下面是一个示例代码: ```python import turtle from datetime import datetime # 获取当前日期和时间 now = datetime.now() current_time = now.strftime("%Y-%m-%d %H:%M:%S") # 创建一个海龟对象 t = turtle.Turtle() # 设置海龟的速度和颜色 t.speed(2) t.pencolor("blue") # 在屏幕上写入日期和时间 t.write(f"当前时间是:{current_time}", align="center", font=("Arial", 16, "bold")) # 保持显示的窗口打开状态 turtle.done() ``` 运行以上代码,你将会看到一个窗口中显示当前的日期和时间。这个窗口会一直保持打开状态,直到你关闭它。 ### 回答3: 要在turtle中显示当前时间,首先需要导入所需的库。例如: ``` import turtle import datetime ``` 然后,我们可以创建一个函数来获取当前时间,并使用turtle库内的方法将其显示在屏幕上。代码如下: ``` def show_current_time(): # 创建一个turtle实例 t = turtle.Turtle() # 获取当前时间 current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 设置turtle的位置和速度 t.penup() t.goto(0, 0) t.speed(1) # 展示当前时间 t.write(current_time, align="center", font=("Arial", 20, "normal")) # 隐藏turtle箭头 t.hideturtle() # 调用函数显示当前时间 show_current_time() ``` 这段代码会在turtle屏幕上显示当前时间时间格式为年-月-日 时:分:秒。turtle实例会以箭头形式指向当前时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值