将datetime转换为只有时间 [英] convert datetime to only time

查看:76
本文介绍了将datetime转换为只有时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在sql中将字段声明为datetime,但是我只希望将时间以hh:mm格式插入.
该字段是日期时间,所以当我转换格式时它要求转换,它再次插入日期和时间
请帮助我..

i have declared a field as datetime in sql but i want only time to be inserted as hh:mm format how to do this.
the field is datetime so it is asking for the conversion when i convert the format it is again inserting the date and time
plz help me..

推荐答案

我想您需要将列更改为varchar()以便以hh:MM格式插入
I guess you need to change the column to varchar() so that you can insert in hh:MM format


检查以下链接:
http://www.csharp-examples.net/string-format-datetime/ [ ^ ]
轻松将字符串转换为DateTime,将DateTime转换为字符串和格式 [ ^ ]
http://stackoverflow.com/questions/1398108/how-do- i-format-a-datetime-in-c [ ^ ]

根据您的要求,我认为以下代码应该可以工作:
Check below links:
http://www.csharp-examples.net/string-format-datetime/[^]
Easy String to DateTime, DateTime to String and Formatting[^]
http://stackoverflow.com/questions/1398108/how-do-i-format-a-datetime-in-c[^]

For your requirement, I think below code should work:
String.Format("{0:g}", dt);  // "3/9/2008 4:05 PM"  ShortDate+ShortTime


这篇关于将datetime转换为只有时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆