简体   繁体   English

如何自定义日期时间格式或将日期时间转换为具有所需格式的字符串

[英]How to customize datetime format or to convert DateTime to String with required format

I have a DateTime string in 24 HOURS format .. "2009-12-31T23:59:59" 我有24小时格式的DateTime字符串..“ 2009-12-31T23:59:59”
I want it in this format .. "12/31/2009 23:59:59" that is: "MM/DD/YYYY HH:MM:SS" 我想要这种格式..“ 12/31/2009 23:59:59”即:“ MM / DD / YYYY HH:MM:SS”

When I tried to covert it to string I am getting "12/31/2009 11:59:59 PM" .. though I can write a code for string manipulation .. it doesn't seem to be the efficient one .. Moreover the case become worst when I have dateTime value like "2009-1-1T1:19:15" .. here, as the string length is varying , I can't even trace the value of Hours and months using substring() and convert.ToInt() .. 当我尝试将其隐式转换为字符串时,我得到的是“ 12/31/2009 11:59:59 PM” ..虽然我可以编写用于字符串操作的代码..它似乎不是有效的..而且当我有dateTime值,例如“ 2009-1-1T1:19:15”时,情况变得最糟。在这里,由于字符串长度在变化 ,我什至无法使用substring()来跟踪Hours和months的值并进行转换.ToInt()..

I am using visual studio 2005, It is throwing error saying "Cannot implicitly convert DateTime to String" when I write this statement: .. 我正在使用Visual Studio 2005,当我编写以下语句时,抛出错误:“无法将DateTime隐式转换为String”:

result = Convert.ToString(dateValue);

I simplify my question: Is there any method to convert "yyyy-mm-ddThh:mm:ss" format to "mm/dd/yyyy hh:mm:ss" .. And it must work in visual studio 2005 .. 我简化我的问题:是否有任何方法可以将“ yyyy-mm-ddThh:mm:ss”格式转换为“ mm / dd / yyyy hh:mm:ss” ..并且它必须在Visual Studio 2005中起作用。

You need to use ParseExact to get it back to datetime: 您需要使用ParseExact将其恢复为日期时间:

string FormatDateTime(string dateString) {
    DateTime dt = DateTime.ParseExact(dateString, "yyyy-MM-ddTHH:mm:ss", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
    return dt.ToString("MM/dd/yyyy HH:mm:ss");
}

That should give you the desired output you are looking for. 这将为您提供所需的输出。 By using InvariantInfo, it will make sure you system settings don't replace the slashes with whatever you have currently defined in the system. 通过使用InvariantInfo,它将确保您的系统设置不会将斜杠替换为您当前在系统中定义的任何内容。 I think I saw in the comments that you had a "." 我想我在评论中看到您有一个“。” being used as your date separator. 用作日期分隔符。

http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.dateseparator.aspx http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.invariantinfo.aspx http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.dateseparator.aspx http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.invariantinfo.aspx

就像是:

string s = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
string FormatDateString(string date_string)
{
    DateTime date;
    if (!DateTime.TryParse(date_string, out date)
    {
        return null;
    }

    return date.ToString("MM/dd/yyyy HH:mm:ss");
}

It's something like this: XmlConvert.ToDateTime(yourDate, "yyyy-MM-dd\\Thh:mm:ss").ToString("MM/dd/yyyy HH:MM:SS") 就像这样: XmlConvert.ToDateTime(yourDate, "yyyy-MM-dd\\Thh:mm:ss").ToString("MM/dd/yyyy HH:MM:SS")

Check here for more info: 在此处查看更多信息:

http://msdn.microsoft.com/en-us/library/kzk5c6y9.aspx http://msdn.microsoft.com/zh-CN/library/kzk5c6y9.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM