转换日期时间的不同格式的特定字符串格式 [英] Convert different format of DateTime to specific String format

查看:84
本文介绍了转换日期时间的不同格式的特定字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时得到 System.datetime.now 由具有不同日期时间格式如下



A mension不同的机器。每个系统

  16月12下午七时25分22秒
16/10/2012下午七时10分47秒[DD / MM / YYYY]
10 /二千零十二分之十六下午7时10分51秒[MM / DD / YYYY]

如何转换不同的格式的DateTime 特定字符串格式的?

 字符串sDateTime = DateTime.Now的ToString(DD / MM / YYYY HH:MM:SS TT); 


解决方案

 如果( DateTime.TryParse(DateTime.Now.ToString(MM / DD / YYYY HH:MM:SS TT),出结果))
sDateTime = DateTime.Now.ToString(MM / DD / YYYY HH:MM :SS TT);
,否则
{
如果(System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern.Equals(DD-MMM-YY))sDateTime = DateTime.Now.ToString( MM / DD / YYYY HH:MM:SS TT);
,否则sDateTime = DateTime.Now.Month +/+ DateTime.Now.Day +/+ DateTime.Now.Year ++ DateTime.Now.ToString(HH:MM:SS TT) ;
}


Im Getting System.datetime.now from different Machine .Each system having different datetime format as a mension below

16-Oct-12 7:25:22 PM
16/10/2012 7:10:47 PM [DD/MM/YYYY]
10/16/2012 7:10:51 PM [MM/DD/YYYY]

How To convert Different format of DateTime to specific String format ?

string sDateTime = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"); 

解决方案

 if (DateTime.TryParse(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt"), out result))
                    sDateTime = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
                else
                {
                    if (System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern.Equals("dd-MMM-yy")) sDateTime = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
                    else sDateTime = DateTime.Now.Month +"/" + DateTime.Now.Day+ "/" + DateTime.Now.Year + " " +  DateTime.Now.ToString("hh:mm:ss tt");
                }

这篇关于转换日期时间的不同格式的特定字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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