将日期和时间字符串转换为yy/mm/dd hh:mm:ss [英] convert date and time string to yy/mm/dd hh:mm:ss

查看:837
本文介绍了将日期和时间字符串转换为yy/mm/dd hh:mm:ss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有这个脚本的脚本,我到达字符串1st是101114(格式yymmdd),日期2nd是162941(hhmmss)时间.

现在要连接这两个字符串,以便我可以将其保存到日期为datetime的sql server中.

但是我不知道将这个字符串转换为dd-mm-yyyy随时间加入.

请帮忙.

谢谢
Seema

Hi,
I have script from this script I get to string 1st is 101114(format yymmdd) date 2nd is 162941 (hhmmss) time.

Now want to join these two strings so that I can save it into sql server dated as datetime.

But I don''t know to convert this string as dd-mm-yyyy join it with time.

Please help.

Thank you
Seema

推荐答案

您应该研究DateTime.ParseExact()或DateTime.Parse()或其中之一的替代.

http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx [ ^ ]

这将为您提供一个可以传递给SqlServer的DateTime.
You should investigate DateTime.ParseExact() or the DateTime.Parse() or one of the overrides of either.

http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx[^]

That will get you a DateTime that can be passed to SqlServer.


谢谢您的提问.您可以按照下面的代码.

Thank you for your question. You can follow the bellow code.

DECLARE @FormatedDateTime varchar(36)
DECLARE @YourDate char(10)
DECLARE @YourTime char(10)

SELECT @YourDate = '101114'
SELECT @YourTime = '162941'

SELECT @FormatedDateTime = convert(varchar, convert(datetime, @YourDate), 111)
    + ' ' + substring(@YourTime, 1, 2)
    + ':' + substring(@YourTime, 3, 2)
    + ':' + substring(@YourTime, 5, 2)

SELECT FormattedDateTime = @FormatedDateTime




谢谢,
Mamun




Thanks,
Mamun


DateTime.ParseExact方法和DateTime.TryParseExact,在您使用稀有格式或混淆DateTime.Parse的格式时很有用.
DateTime.ParseExact method and DateTime.TryParseExact, are useful when you have a rare format or a format that confuses DateTime.Parse.
using System;
using System.Globalization;
class Program
{
    static void Main()
    {
        string dateString = "Mon 16 Jun 8:30 AM 2008"; 
        string format = "ddd dd MMM h:mm tt yyyy";
        DateTime dateTime = DateTime.ParseExact(dateString, format,
            CultureInfo.InvariantCulture);
        Console.WriteLine(dateTime);
    }
}



希望这个例子对您有帮助.



Hope this example will help you.


这篇关于将日期和时间字符串转换为yy/mm/dd hh:mm:ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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