C#如何字符串转换成使用时间范围时间显示格式? [英] C# How to convert String into Time format used for Time Range?

查看:155
本文介绍了C#如何字符串转换成使用时间范围时间显示格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,能够检索使用标记化日志文本文件的时间段/一部分。



该方案的主要目的是要检索的时间的一部分,然后继续进行到字符串转换为DateTime格式可能被随后用作时间范围的一部分时间线功能。



然而在转换的时候到日期时间,系统将输出结果放到
23/11/2010下午9点31分○○秒,这正确的时间转换为12小时格式,而是采用了日期功能。



因此,问题是如何只转换的时间,而不是输出或处理的日期。而如何能在时间转换为24小时格式为HH:MM:SS



请在代码的建议。谢谢!



 类节目
{
静态无效的主要(字串[] args)
{

//System.Collections.Generic.IEnumerable<String>线= File.ReadLines(C:\\Test\\\\
tfs2.txt);

字符串值=星期四1995年3月2日21:31:00,2245107,男... R / rrwxrwxrwx,0,0,8349-128-3,C:/ Program Files文件/ AccessData / AccessData法医工具包/程序/共发现/ Adj.dat

//的foreach(在线串R)
// {

字符串[]标记= value.Split(,);

的String []日期时间=令牌[0] .Split('');

字符串timeText =日期时间[4]; //字符串数组contans 21时31分〇〇秒

DateTime的时间= Convert.ToDateTime(timeText); //转换只是时间

Console.WriteLine(时间);

}
}


解决方案

  //System.Collections.Generic.IEnumerable<String>线= File.ReadLines(C:\\Test\\\\
tfs2.txt);
字符串值=星期四1995年3月2日21:31:00,2245107,男... R / rrwxrwxrwx,0,0,8349-128-3,C:/ Program Files文件/ AccessData / AccessData法医工具包/Program/wordnet/Adj.dat;

的String []标记= value.Split('');

的String []日期时间=令牌[0] .Split('');

字符串timeText =日期时间[4]; //字符串数组contans 21时31分〇〇秒

DateTime的时间= Convert.ToDateTime(timeText); //转换只是时间

Console.WriteLine(time.ToString(HH:MM:SS));

您可以使用则DateTime.ToString(模式)为DateTime转换为任何所需的格式。



有可用这里的模式列表的 http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm


I have a program that is able to retrieve sections/part of a log text file's time using tokenization.

The main aim of the program which is to retrieve the time part then proceed onto converting the string to a DateTime format which could be then used as a part of a Time Range timeline function.

However while converting the time into DateTime, the system outputs the results into "23/11/2010 9:31:00 PM" which correctly converts the time into at 12 Hour format but utilizes the Date function.

Therefore the question would be how to only convert the time and NOT output or process the date. And how can the time be converted into a 24 Hour format into HH:MM:SS?

Please advice on the codes. Thanks!

class Program
{
    static void Main(string[] args)
    {

        //System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("C:\\Test\\ntfs2.txt");

        String value = "Thu Mar 02 1995 21:31:00,2245107,m...,r/rrwxrwxrwx,0,0,8349-128-3,C:/Program Files/AccessData/AccessData Forensic Toolkit/Program/wordnet/Adj.dat";

        //foreach (String r in lines)
        //{

        String[] token = value.Split(',');

        String[] datetime = token[0].Split(' ');

        String timeText = datetime[4]; // The String array contans 21:31:00

        DateTime time = Convert.ToDateTime(timeText); // Converts only the time

        Console.WriteLine(time);

    }
}

解决方案

    //System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("C:\\Test\\ntfs2.txt");
    String value = "Thu Mar 02 1995 21:31:00,2245107,m...,r/rrwxrwxrwx,0,0,8349-128-3,C:/Program Files/AccessData/AccessData Forensic Toolkit/Program/wordnet/Adj.dat";

    String[] token = value.Split(',');

    String[] datetime = token[0].Split(' ');

    String timeText = datetime[4]; // The String array contans 21:31:00

    DateTime time = Convert.ToDateTime(timeText); // Converts only the time

    Console.WriteLine(time.ToString("HH:mm:ss"));

You can use DateTime.ToString("pattern") to convert a DateTime to any desired format.

There's a list of patterns available here http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

这篇关于C#如何字符串转换成使用时间范围时间显示格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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