Sql与C#中日期格式转换总结
SQL中的转换方法:
一、将string转换为datetime,主要是使用Convert方法,
方法,Convert(datetime [ ( length ) ] , expression, [style])
如:convert(datetime,Datetime.Now,120)结果如: 2014-09-16 15:47:18
格式说明: -- yyyy-mm-dd hh:mm:ss(24h)
二、将datetime转换为string
方法: Convert(char [ ( length ) ], CURRENT_TIMESTAMP, [style]) 或 Convert(nvarchar(50) , getdate(), 106 )
如: Convert(char[(19)], Datetime.Now, 20)结果如: 2014-09-16 15:50:13
格式说明:-- yyyy-mm-dd hh:mm:ss(24h)
在C#代码中的转换方法:
一、将string转换为datetime.
方法一、Convert.ToDateTime(string) //将日期和时间的指定 String表示形式转换为等效的 DateTime。
如:Convert.ToDateTime(Datetime.Now),结果如:2014/9/16 下午3:54:14
方法二、 DateTime.Parse(string) //将日期和时间的指定字符串表示转换成其等效的 DateTime。
如:DateTime.Parse(Datetime.Now), 结果如:2014/9/16 下午3:54:14
二、将datetime转换为string.
方法一、DateTime.ToString (String) //使用指定的格式将当前 DateTime对象的值转换为它的等效字符串表示形式。
如: DateTime.ToString (Datetime.Now), 结果如:2014/9/16 下午3:54:14
常这样使用: DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")
注:HH:mm:ss 为二十四小时制。
附一: DateTime.ToString ()格式化说明:
|
d |
月中的某一天 |
一位数的日期没有前导零 |
|
dd |
月中的某一天 |
一位数的日期有一个前导零 |
|
ddd |
周中某天的缩写名称 |
在 AbbreviatedDayNames 中定义 |
|
dddd |
周中某天的完整名称 |
在 DayNames 中定义 |
|
M |
月份数字 |
一位数的月份没有前导零 |
|
MM |
月份数字 |
一位数的月份有一个前导零 |
|
MMM |
月份的缩写名称 |
在 AbbreviatedMonthNames 中定义 |
|
MMMM |
月份的完整名称 |
在 MonthNames 中定义 |
|
y |
不包含纪元的年份 |
如果不包含纪元的年份小于 10,则显示不具有前导零的年份 |
|
yy |
不包含纪元的年份 |
如果不包含纪元的年份小于 10,则显示具有前导零的年份 |
|
yyyy |
包括纪元的四位数的年份 |
|
|
h |
12 小时制的小时 |
一位数的小时数没有前导零 |
|
hh |
12 小时制的小时 |
一位数的小时数有前导零 |
|
H |
24 小时制的小时 |
一位数的小时数没有前导零 |
|
HH |
24 小时制的小时 |
一位数的小时数有前导零 |
|
m |
分钟 |
一位数的分钟数没有前导零 |
|
mm |
分钟 |
一位数的分钟数有一个前导零 |
|
s |
秒 |
一位数的秒数没有前导零 |
|
ss |
秒 |
一位数的秒数有一个前导零 |
附二:Convert(datetime [ ( length ) ] , expression, [style])格式说明:
convert(datetime, 'Oct 23 2014 11:01AM', 100) -- mon dd yyyy hh:mmAM (or PM)
convert(datetime, '10/23/2014', 101) -- mm/dd/yyyy
convert(datetime, '2014.10.23', 102) -- yyyy.mm.dd
convert(datetime, '23/10/2014', 103) -- dd/mm/yyyy
convert(datetime, '23.10.2014', 104) -- dd.mm.yyyy
convert(datetime, '23-10-2014', 105) -- dd-mm-yyyy
convert(datetime, '23 OCT 2014', 106) -- dd mon yyyy
convert(datetime, 'Oct 23, 2014', 107) -- mon dd, yyyy
convert(datetime, '20:10:44', 108) -- hh:mm:ss
convert(datetime, 'Oct 23 2014 11:02:44:013AM', 109)
convert(datetime, '10-23-2014', 110) -- mm-dd-yyyy
convert(datetime, '2014/10/23', 111) -- yyyy/mm/dd
convert(datetime, '20141023', 112) -- yyyymmdd
convert(datetime, '23 Oct 2014 11:02:07:577', 113) -- dd mon yyyy hh:mm:ss:mmm
convert(datetime, '20:10:25:300', 114) -- hh:mm:ss:mmm(24h)
convert(datetime, '2014-10-23 20:44:11', 120) -- yyyy-mm-dd hh:mm:ss(24h)
convert(datetime, '2014-10-23 20:44:11.500', 121) -- yyyy-mm-dd hh:mm:ss.mmm
convert(datetime, '2014-10-23T18:52:47.513', 126) -- yyyy-mm-ddThh:mm:ss.mmm
附三: Convert(char [ ( length ) ], CURRENT_TIMESTAMP, [style])格式化说明:
|
0 |
Feb 22 2014 4:26PM |
Convert(CHAR(19), CURRENT_TIMESTAMP, 0) |
|
1 |
02/22/14 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 1) |
|
2 |
14.02.22 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 2) |
|
3 |
22/02/14 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 3) |
|
4 |
22.02.14 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 4) |
|
5 |
22-02-14 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 5) |
|
6 |
22 Feb 14 |
Convert(CHAR(9), CURRENT_TIMESTAMP, 6) |
|
7 |
Feb 22, 14 |
Convert(CHAR(10), CURRENT_TIMESTAMP, 7) |
|
8 |
16:26:08 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 8) |
|
9 |
Feb 22 2014 4:26:08:020PM |
Convert(CHAR(26), CURRENT_TIMESTAMP, 9) |
|
10 |
02-22-14 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 10) |
|
11 |
14/02/22 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 11) |
|
12 |
140222 |
Convert(CHAR(6), CURRENT_TIMESTAMP, 12) |
|
13 |
22 Feb 2014 16:26:08:020 |
Convert(CHAR(24), CURRENT_TIMESTAMP, 13) |
|
14 |
16:26:08:037 |
Convert(CHAR(12), CURRENT_TIMESTAMP, 14) |
|
20 |
2014-02-22 16:26:08 |
Convert(CHAR(19), CURRENT_TIMESTAMP, 20) |
|
21 |
2006-02-22 16:26:08.037 |
Convert(CHAR(23), CURRENT_TIMESTAMP, 21) |
|
22 |
02/22/14 4:26:08 PM |
Convert(CHAR(20), CURRENT_TIMESTAMP, 22) |
|
23 |
2014-02-22 |
Convert(CHAR(10), CURRENT_TIMESTAMP, 23) |
|
24 |
16:26:08 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 24) |
|
25 |
2014-02-22 16:26:08.037 |
Convert(CHAR(23), CURRENT_TIMESTAMP, 25) |
|
100 |
Feb 22 2014 4:26PM |
Convert(CHAR(19), CURRENT_TIMESTAMP, 100) |
|
101 |
02/22/2014 |
Convert(CHAR(10), CURRENT_TIMESTAMP, 101) |
|
102 |
2014.02.22 |
Convert(CHAR(10), CURRENT_TIMESTAMP, 102) |
|
103 |
22/02/2014 |
Convert(CHAR(10), CURRENT_TIMESTAMP, 103) |
|
104 |
22.02.2014 |
Convert(CHAR(10), CURRENT_TIMESTAMP, 104) |
|
105 |
22-02-2014 |
Convert(CHAR(10), CURRENT_TIMESTAMP, 105) |
|
106 |
22 Feb 2014 |
Convert(CHAR(11), CURRENT_TIMESTAMP, 106) |
|
107 |
Feb 22, 2014 |
Convert(CHAR(12), CURRENT_TIMESTAMP, 107) |
|
108 |
16:26:08 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 108) |
|
109 |
Feb 22 2014 4:26:08:067PM |
Convert(CHAR(26), CURRENT_TIMESTAMP, 109) |
|
110 |
02-22-2014 |
Convert(CHAR(10), CURRENT_TIMESTAMP, 110) |
|
111 |
2014/02/22 |
Convert(CHAR(10), CURRENT_TIMESTAMP, 111) |
|
112 |
20140222 |
Convert(CHAR(8), CURRENT_TIMESTAMP, 112) |
|
113 |
22 Feb 2014 16:26:08:067 |
Convert(CHAR(24), CURRENT_TIMESTAMP, 113) |
|
114 |
16:26:08:067 |
Convert(CHAR(12), CURRENT_TIMESTAMP, 114) |
|
120 |
2014-02-22 16:26:08 |
Convert(CHAR(19), CURRENT_TIMESTAMP, 120) |
|
121 |
2014-02-22 16:26:08.080 |
Convert(CHAR(23), CURRENT_TIMESTAMP, 121) |
|
126 |
2014-02-22T16:26:08.080 |
Convert(CHAR(23), CURRENT_TIMESTAMP, 126) |
|
127 |
2014-02-22T16:26:08.080 |
Convert(CHAR(23), CURRENT_TIMESTAMP, 127) |
|
130 |
24 ???? 1427 4:26:08:080PM |
Convert(CHAR(32), CURRENT_TIMESTAMP, 130) |
|
131 |
24/01/1427 4:26:08:080PM |
Convert(CHAR(25), CURRENT_TIMESTAMP, 131) |
Sql与C#中日期格式转换总结的更多相关文章
- 【网络收集】Sql Server datetime 常用日期格式转换
) , sfrq, ) 我们经常出于某种目的需要使用各种各样的日期格式,当然我们可以使用字符串操作来构造各种日期格式,但是有现成的函数为什么不用呢? SQL Server中文版的默认的日期字段date ...
- layui的table中日期格式转换
我使用的layui的2.4.5版本,在util中已经带了格式转换的函数.这里主要记录如何使用. 未使用前在table中显示如下: 前端代码如下: {field:'createDate', title: ...
- SQL 将非标准日期格式转换成标准格式,进行条件判断
a.JLDate为非标准日期格式: 例: 2011-8-28 0:00:000011-8-28 0:00:000111-8-4 0:00:00 select CONVERT(varchar(50),C ...
- js读取excel中日期格式转换问题
在使用js-xlsx插件来读取excel时,会将2018/10/16这种数据自动装换成48264.12584511. 所以需要自己手动再转换回来 // excel读取2018/01/01这种时间格式是 ...
- JAVA中日期格式转换各个字母代表含义
G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Month July; Jul; 07 w 年中的周数 Number 27 W ...
- linq中日期格式转换或者比较,程序报错说不支持方法的解决办法
public void TestMethod1(){using (var _context = new hotelEntities()){var rq = DateTime.Now.Date;var ...
- java 实体类中日期格式转换
@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss&quo ...
- 在SQL Server中 获取日期、日期格式转换
--常用日期转换参数: PRINT CONVERT(varchar, getdate(), 120 ) 2016-07-20 16:09:01 PRINT replace(replace(replac ...
- java中日期格式的转换和应用
java中主要有3个类用于日期格式转换 DateFormat .SimpleDateFormat.Calendar SimpleDateFormat函数的继承关系: java.lang.Obje ...
随机推荐
- MinGW GCC 6.3.0 2017年3月份出炉啦
MSYS_MinGW-w64_GCC_630_x86-x64_Full 发布日期: 2017-03-07 08:48 68264 KB 下载地址: http://xhmikosr.1f0.de/too ...
- 016_把普通用户免秘钥加入root用户的几种方式
一.第一种方式. (1) [root@infra-jyallkv-tikv-pps-7 ~]# tail /etc/sudoers## Allows members of the users grou ...
- OpenStack实践系列⑨云硬盘服务Cinder
OpenStack实践系列⑨云硬盘服务Cinder八.cinder8.1存储的三大分类 块存储:硬盘,磁盘阵列DAS,SAN存储 文件存储:nfs,GluserFS,Ceph(PB级分布式文件系统), ...
- Json 文件中value的基本类型
在Json中,value的类型只能是以下几种: 1.字符串 2.数字 3.true 或者 false (注意,和字符串不同,没有双引号包裹) 4.null
- Boostrap轮图片可以左右滑动
记得引用Boostrap的js和css html代码: <div id="Mycarousel" class="carousel slide col-md-12&q ...
- 在v-html中,js 正则表达式清除字符串里的style属性
项目中遇到这样的需求,后端返回的是字符串,在vue用v-html显示,里面有style样式,要去除style 在v-html中使用filters,和平时的不一样,推荐项目的方法,定义一个全局的过滤方法 ...
- liunx 利用nginx 实现负载均衡
一般采用软件实现负载均衡的有Nginx.apache.nginx 近年来使用频繁,其官网上面显示可以承载5万并发访问量,太牛了. nginx 相比 apache优势明显:Nginx 服务程序比较稳定, ...
- Linux Oracle bash: “sqlplus / as sysdba”: command not found 解决方法
bash: sqlplus: command not found 解决方法 注:本文来源于 < bash: sqlplus: command not found 解决方法 > 1: ...
- Linux磁盘与文件系统管理笔记
### Linux磁盘与文件系统管理 linux 最传统的文件系统格式是EXT2,centos7 默认文件系统是xfs(日志式文件系统) 磁盘的组成: 盘片 机械手臂 主轴马达 (机械硬盘) 磁盘格式 ...
- Data Preprocess
本文试图解决一个问题,即我们自定义的数据如何训练模型?初识深度学习,我们接触手写数字识别模型,但是批次数据是mnist已经定义好的,我们现在有自己的图片如何做成批次进行训练模型. 现在我们将准备好的原 ...