【发布时间】:2019-05-16 01:14:29
【问题描述】:
所以我使用的是 Visual Studio 2017,并使用 .Net 表单。我正在尝试制作一个以以下格式提供时间的时钟 7 小时-分钟-秒,一周中的年-周
我可以很好地设置年、小时、分钟和秒的变量,因为这是一个基本的东西。但是,我无法找到一种方法来获取一年中的星期和 7 日的日期。
所以,我已经尝试使用简单的数学 365-DateTime.now.day 并除以 7 来计算一年中的一周。它非常混乱,我得到了一个错误日志(duh)
我对一周中的每一天都做了同样的事情,但使用上面的周公式作为开始。
我知道我没有尝试太多,但我对 c# 还是很陌生。它是我为学习而制作的应用程序。我知道日期时间部分缺少代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
Timer t = new Timer();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//timer interval
t.Interval = 1000; //in milliseconds
t.Tick += new EventHandler(this.t_tick);
//start timer when form loads
t.Start(); //this will use t_tick() method
}
//timer interval
private void t_tick(object sender,EventArgs e)
{
//get current time
int yy = DateTime.Now.year;
int ww = DateTime.Now.//missing code,get the week of the current year??
int wd = DateTime.Now.DayOfWeek; //on 7 days??
int hh = DateTime.Now.Hour;
int mm = DateTime.Now.Minute;
int ss = DateTime.Now.Second;
//time
String time = "";
//padding leading
if(hh < 10)
{
time += "0" + hh;
}
else
{
time += hh;
}
time += ":";
if (mm < 10)
{
time += "0" + mm;
}
else
{
time += mm;
}
time += ":";
if (ss < 10)
{
time += "0" + ss;
}
else
{
time += ss;
}
//update label
label1.Text = time;
}
}
}
所以,预期的结果很简单。我想如上所述输出日期和时间。实际结果是什么,因为我的代码不完整。
注意:我知道稍后我需要更改“hh
【问题讨论】:
-
那么问题是什么???它是关于一年中的一周还是与时间相关的部分? (提示:DateTime 已经将时间作为时间,您可以使用 ToString 打印您想要的任何部分)。 WeekOfYr 更复杂。 1 月 1 日是星期二,这算一整周吗?星期六呢?这是 ISO WeekOfYear 吗?
-
哦,对不起,我不清楚哈哈我想将星期日输出为 1,星期一输出为 2 等等,我希望它输出 1 月 1 日为第 1 周。就像电影“及时”中的生命倒计时一样" 但是在倒计时的地方,当前时间,我不知道如何在代码中获取这些
-
回答你的第一个问题,我希望它是日历上的当前星期几,意思是如果 1 月 1 日是星期二,那么无论如何都是第 1 天
-
visual-studio标签只应在问题是关于 Visual Studio 时使用。关于代码的问题不应该使用 VS 标签。 See the tag info