写在前面

最近遇到的一个问题,在英文操作系统上,获取到的时间是带am或者pm的。但是数据库是datetime类型,存储的时候竟然都变成0000-00-00 00:00:00.但是在中文操作系统上又是正常的。没办法只有转换了。

解决方案

其实很多时候Convert.ToDateTime()这个方法完全满足需求。可有些地方确实比较蛋疼,这里还是记录一下,加深印象。

方式一

    class Program
{
static void Main(string[] args)
{
// Summary:
// Initializes a new instance of the System.Globalization.CultureInfo class
// based on the culture specified by name and on the Boolean that specifies
// whether to use the user-selected culture settings from the system.
//
// Parameters:
// name:
// A predefined System.Globalization.CultureInfo name, System.Globalization.CultureInfo.Name
// of an existing System.Globalization.CultureInfo, or Windows-only culture
// name. name is not case-sensitive.
//
// useUserOverride:
// A Boolean that denotes whether to use the user-selected culture settings
// (true) or the default culture settings (false).
System.Globalization.DateTimeFormatInfo dtInfo = new System.Globalization.CultureInfo("en-Us",true).DateTimeFormat;
//Gets or sets the custom format string for a short time value.
dtInfo.ShortTimePattern = "t";
DateTime dt = DateTime.Parse("05-28-15 03:07PM ", dtInfo);
Console.WriteLine(dt);
Console.Read();
}

输出

方式二

            string strDt = "2014-03-02 03:07PM";
strDt = strDt.Trim("PM".ToCharArray());
DateTime dt =Convert.ToDateTime(strDt).AddHours();
Console.WriteLine(dt);

总结

这也是在开发中遇到的一个问题,觉得好奇,就查看了下实现方式,遇到了,就记录下吧。

am,pm时间转换的更多相关文章

  1. 将形如:Oct 8, 2016 5:29:44 PM串转换成正常时间在真机上遇到的坑

    将形如:Oct 8, 2016 5:29:44 PM串转换成正常时间在真机上遇到的坑 /** * 根据传入字符串 * * @param str 传入的日期字符串 形如:Oct 8, 2016 5:29 ...

  2. unix环境C编程之日期时间转换

    1.理清概念 1.1.日历时间:   含义:国际标准时间1970年1月1日00:00:00以来经过的秒数.   数据类型:time_t.实际上是long的别名. 1.2.tm结构时间:   含义:结构 ...

  3. Java 时间转换问题总结

    这几天开发中遇到时间转换出错的问题,特总结如下:   ========================================================================= ...

  4. mysql 时间函数 时间转换函数

    时间函数 Now 获取当前时间 current_timestamp 获取当前时间 localtimestamp 时间转换 UNIX_TIMESTAMP    "2009-09-15 00:0 ...

  5. java日期和时间转换字符

    日期和时间转换字符 字符 描述 例子 c 完整的日期和时间 Mon May 04 09:51:52 CDT 2009 F ISO 8601 格式日期 2004-02-09 D U.S. 格式日期 (月 ...

  6. [jquery]将当前时间转换成yyyymmdd格式

    如题: function nowtime(){//将当前时间转换成yyyymmdd格式 var mydate = new Date(); var str = "" + mydate ...

  7. MySQL 日期、时间转换函数

    MySQL 日期.时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式.它是 str_to ...

  8. java时间类型的转换/获取当前时间/将时间转换成String/将String转换成时间

    对于我的脑子,我已经服气了...写了N遍的东西,就是记不住...既然记不住那就记下来... 利用java获取当前的时间(String类型,年-月-日 时:分:秒) //我要获取当前的日期 Date d ...

  9. inner join ,left join ,right join 以及java时间转换

    1.inner join ,left join 与 right join (from 百度知道) 例表aaid adate1    a12    a23    a3表bbid  bdate1     ...

随机推荐

  1. 使用NDK c++建立一个Android应用

    使用NDK c++建立一个Android应用 一.工具 ADT(集成了eclipse,cdt,ndk plug-in) NDK (用它来编译c/c++程序) JDK (Java开发包) ANT(ecl ...

  2. java String部分源码解析

    String类型的成员变量 /** String的属性值 */ private final char value[]; /** The offset is the first index of the ...

  3. Add Binary

    Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...

  4. splice()函数,'SPLICE_F_MOVE' 'SPLICE_F_NONBLOCK' 'SPLICE_F_MORE' undeclared

    1.编译含有splice()函数的程序时出现,'SPLICE_F_MOVE'  undeclared,'SPLICE_F_NONBLOCK' ‘SPLICE_F_MORE' 也是一样undeclare ...

  5. 矩阵乘法 codevs 1287 矩阵乘法

    1287 矩阵乘法  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold       题目描述 Description 小明最近在为线性代数而头疼,线性代数确实很抽象 ...

  6. 票据OCR前预处理 (附Demo)

    发一个去年做的一个去除票据干扰项的demo,核心处理是移除红色印章,不破坏红印叠加处的文字. 只是一个小小demo,还没具体进行进一步优化. 也不知道什么时候才有精力继续优化它. 现在放出来给大家试用 ...

  7. 在mac os下的Apache服务器的cgi中运行python

    我是搬运工.. Running Python Programs on the Mac OS X Apache Web Server The Mac OS X operating system incl ...

  8. 2014 Super Training #4 D Paint the Grid Again --模拟

    原题:ZOJ 3780 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 刚开始看到还以为是搜索题,没思路就跳过了.结 ...

  9. [3D跑酷] GUIManager UI管理

    UI元素更新及界面跳转 继上篇日志<Unity开发之 GUIClickEventReceiver>,再谈一下我们如何管理游戏中的UI元素更新及界面跳转 UI绑定 图一:Inspector面 ...

  10. 15Spring_AOP编程(AspectJ)_抛出通知