VS C# 获取系统当前时间+timer的作用+无法将类型“int”隐式转换为“string”;
来源:互联网 发布:淘宝联盟一淘同时使用 编辑:程序博客网 时间:2024/05/21 18:39
总结一下今天遇到的一个小问题;
在敲机房重构的时候因为要在主窗体上显示一个时间,当时第一反应就是从系统获取,之前在学VB的时候印象中有过,不过现在是C#,需要一个全新的想法来做,下面是我的第一版代码:
<span style="font-family:KaiTi_GB2312;font-size:18px;"> private void frmMain_Load(object sender, EventArgs e) { System.DateTime currentTime = new System.DateTime(); currentTime = System.DateTime.Now; LblTime.Text =currentTime.Year + "年" + currentTime.Month + "月" + currentTime.Day + "日" + currentTime.Hour + "时" + currentTime.Minute + "分" + currentTime.Second + "秒"; }</span>
后来上网查了好久,感觉没有出错啊,最后才知道一个小细节给忽略了,一直到现在,说真的没有怪自己,因为在此之前自己也是真的不知道;
OK,下面是正确的代码:
<span style="font-family:KaiTi_GB2312;font-size:18px;"> private void frmMain_Load(object sender, EventArgs e) { System.DateTime currentTime = new System.DateTime(); currentTime = System.DateTime.Now; LblTime.Text = "" + currentTime.Year + "年" + currentTime.Month + "月" + currentTime.Day + "日" + currentTime.Hour + "时" + currentTime.Minute + "分" + currentTime.Second + "秒"; }</span>
有看出哪里不一样么?
我画图标注一下:
两个小小的引号引发的时间,耽搁了不少时间,不过也好,学到东西了。
殊不知,这样完成后以为OK了,就在自己陶醉在哇,一个漂亮的小功能实现了哎!
定睛一看,妈呀,时间咋不走字呢?
时间信息是从系统里调出来了,不过调出来是几点,它会一直是几点钟,后来就像,得让它一直刷新,于是第三版代码就出来了:
<span style="font-family:KaiTi_GB2312;font-size:18px;"> private void timer1_Tick(object sender, EventArgs e) { System.DateTime currentTime = new System.DateTime(); currentTime = System.DateTime.Now; LblTime.Text = "" + currentTime.Year + "年" + currentTime.Month + "月" + currentTime.Day + "号" + currentTime.Hour + "点" + currentTime.Minute + "分" + currentTime.Second + "秒"; }</span>
有看出有什么不一样了么?
引用了一个小表,“timer” ,很简单吧,只是刚刚弄出来感觉挺有意思,就写篇博客记录一下,以资鼓励!
OK,画图标注一下:
这篇博客就先到这里了,下次继续总结!
如果读者您有什么问题或者建议,非常欢迎一起交流。
0 0
- VS C# 获取系统当前时间+timer的作用+无法将类型“int”隐式转换为“string”;
- 无法将类型"int"隐式转换为"string"
- 无法将类型“string”隐式转换为“int”的处理办法
- 将String类型转换为Int类型
- C# 无法将类型为“System.DBNull”的对象强制转换为类型“System.String”。
- 关于无法将类型“object”隐式转换为“string”
- 无法将类型“string”隐式转换为“char
- 将int转换为string类型
- C# 获取当前时间戳,将时间戳转换为当前本地时间
- “无法将类型"string"转换为"char"”?
- 错误 2 无法将类型“int”隐式转换为“bool”
- VS ---- 无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState"
- 无法将类型为“System.Int32”的对象强制转换为类型“System.String”
- String转换为int类型
- 无法将类型“string”隐式转换为“System.Data.SqlClient.SqlParameter”(错误)
- 无法将类型“string”隐式转换为 “System.Windows.Forms.DataGridViewTextBoxColumn 解决
- c#无法从“方法组”转换为“String”或无法从“int”转换为“string”
- js将时间由long类型转换为string类型
- 2016多校训练Contest6: 1001 A Boring Question hdu5793
- JS中的onload事件和jQuery中的ready事件,如何用JS实现jQuery中的ready
- Android 6.0 运行时权限检查机制
- PropertyPlaceholderConfigurer 用法
- SVN 使用详解
- VS C# 获取系统当前时间+timer的作用+无法将类型“int”隐式转换为“string”;
- 求大组合数取模的模板(lucas定理)
- kvm module之create vcpu
- 强制横屏的一种实现 ios
- 打开多个Activity退出的方法
- (转)Fragment的startActivityForResult
- POJ 1704 Georgia and Bob (博弈 Nim游戏变形)
- JavaScript代码实现图片延迟加载并淡入淡出显示效果
- 行人检测方法代码汇总