C#中时间的使用、SQL中时间的使用

DateTime 数字型 ......
1
DateTime 数字型 
System.DateTime currentTime=new System.DateTime(); 
1.1 
取当前年月日时分秒 
currentTime=System.DateTime.Now; 
1.2 
取当前年 
int 
=currentTime.Year; 
1.3 
取当前月 
int 
=currentTime.Month; 
1.4 
取当前日 
int 
=currentTime.Day; 
1.5 
取当前时 
int 
=currentTime.Hour; 
1.6 
取当前分 
int 
=currentTime.Minute; 
1.7 
取当前秒 
int 
=currentTime.Second; 
1.8 
取当前毫秒 
int 
毫秒=currentTime.Millisecond; 
(变量可用中文) chinaitpower.comNJMNpKx60pHmoHa 
1.9 
取中文日期显示——年月日时分 
string strY=currentTime.ToString("f"); //
不显示秒 chinaitpower.comNJMNpKx60pHmoHa

1.10 
取中文日期显示_年月 
string strYM=currentTime.ToString("y"); chinaitpower.comNJMNpKx60pHmoHa

1.11 
取中文日期显示_月日 
string strMD=currentTime.ToString("m"); chinaitpower.comNJMNpKx60pHmoHa

1.12 
取当前年月日,格式为:2003-9-23 
string strYMD=currentTime.ToString("d"); chinaitpower.comNJMNpKx60pHmoHa

1.13 
取当前时分,格式为:1424 
string strT=currentTime.ToString("t"); chinaitpower.comNJMNpKx60pHmoHa

2
Int32.Parse(变量) Int32.Parse("常量") 
字符型转换 转为32位数字型 chinaitpower.comNJMNpKx60pHmoHa

 

smalldatetime类型中怎么查找到是某一年的记录出来

我的数据表是如下的:  
  attdate                                     name  
  ------------------------------  
  2001-4-2   12:00:00                 cheng  
  2002-12-2   12:00:00               zhang  
  2003-12-2   12:00:00               huang  
  2002-12-2   12:00:00               zhang  
  ……  
  ------------------------------  
  attdate
smalldatetime型的.怎么从数据库中取出2002年的所有记录出来呢?

 

select   *   from     where   year(attdate)   =   2002  
   
  select   *   from  
  where   convert(char(4),attdate,120)   =   '2002'  
   
  select   *   from  
  where   datepard(year,attdate)   =   2002  
   
  select   *   from  
  where   datediff(year,'2002-01-01',attdate)   =   0

 

 

如果要查哪一年哪一月的呢?  
  ----------------------------------------------------------------  
 
比如查询20026  
   
  select   *   from  
  where   year(attdate)   =   2002   and   month(attdate)   =   6  
   
  select   *   from  
  where   convert(char(7),attdate,120)   =   '2002-06'  
   
  select   *   from  
  where   datepard(year,attdate)   =   2002   and   datepard(month,attdate)   =   6  
   
  select   *   from  
  where   datediff(month,'2002-06-01',attdate)   =   0

 

取出来时间格式

select   CONVERT(varchar(10),   getDate(),120)   --不要时间2002-1-1  
  select   convert(char(8),getdate(),112)   ----20020101  
  select   convert(char(8),getdate(),108)     ---06:05:05  
   
    101  
美国   mm/dd/yyyy    
  2   102   ANSI   yy.mm.dd    
  3   103  
英国/法国   dd/mm/yy    
  4   104  
德国   dd.mm.yy    
  5   105  
意大利   dd-mm-yy    
  6   106   -   dd   mon   yy    
  7   107   -   mon   dd,   yy    
  8   108   -   hh:mm:ss    
  -   9  
  109   (*)     默认值   +   毫秒   mon   dd   yyyy   hh:mi:ss:mmmAM(或   PM    
  10   110  
美国   mm-dd-yy    
  11   111  
日本   yy/mm/dd    
  12   112   ISO   yymmdd    
  -   13  
  113   (*)     欧洲默认值   +   毫秒   dd   mon   yyyy   hh:mm:ss:mmm(24h)    
  14   114   -   hh:mi:ss:mmm(24h)    
  -   20  
  120   (*)     ODBC   规范   yyyy-mm-dd   hh:mm:ss[.fff]    
  -   21  
  121   (*)     ODBC   规范(带毫秒)   yyyy-mm-dd   hh:mm:ss[.fff]    
  -   126(***)   ISO8601   yyyy-mm-dd   Thh:mm:ss:mmm
(不含空格)    
  -   130*  
科威特   dd   mon   yyyy   hh:mi:ss:mmmAM    
  -   131*  
科威特   dd/mm/yy   hh:mi:ss:mmmAM

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值