我有一个程序能够使用标记化检索日志文本文件的部分/部分时间。
该程序的主要目的是检索时间部分,然后继续将字符串转换为DateTime格式,然后将其用作时间范围时间轴函数的一部分。
然而,在将时间转换为DateTime时,系统会将结果输出到 “23/11/2010 9:31:00 PM”正确地将时间转换为12小时格式,但使用了日期功能。
因此,问题是如何只转换时间和非输出或处理日期。如何将时间转换为24小时格式为HH:MM:SS?
请提供有关代码的建议。谢谢!
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);
}
}
答案 0 :(得分:7)
//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"));
您可以使用DateTime.ToString(“pattern”)将DateTime转换为任何所需的格式。
这里有一个模式列表http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm
答案 1 :(得分:2)
只需使用此代码将日期时间转换为5.12 pm至1712
public string ConvDate_as_str(string dateFormat)
{
try
{
char[] ch = dateFormat.ToCharArray();
string[] sps = dateFormat.Split(' ');
string[] spd = sps[0].Split('.');
dateFormat = spd[0] + ":" + spd[1];
if ((sps[1].ToUpper() != "PM") && (sps[1].ToUpper() != "AM"))
{
return "Enter Correct Format like <5.12 pm>";
}
DateTime dt = new DateTime();
dt = Convert.ToDateTime(dateFormat);
string date = "";
if (sps[1].ToUpper() == "PM")
{
date = (dt.Hour + 12).ToString("00");
}
else
date = dt.Hour.ToString("00");
return date + dt.Minute.ToString("00");
}
catch (Exception ex)
{
return "Enter Correct Format like <5.12 pm>";
}
}
答案 2 :(得分:1)
//your log record
string record = "Thu Mar 02 1995 21:31:00,2245107...";
//get the first part - you can include the Thu Mar 02 1995 (datetime can parse)
string date = record.Split(',')[0];
//parse to date time (the Thu Mar 02 doesn't phase dateTime)
DateTime dt = DateTime.Parse(date);
//convert the datetime to a string HH = 24 Hours leading zero, mm = minutes, ss = seconds
string time = dt.ToString("HH:mm:ss");
//write!
Console.WriteLine(time);
编辑我看到你说不处理日期,这在技术上处理它 - 但它比基于空格拆分和逐段重建时间要清晰得多。
答案 3 :(得分:0)
DateTime x = DateTime.Parse("01/01/2010 10:45 PM");
Console.WriteLine(x.ToString("HH:mm"));
答案 4 :(得分:0)
只需使用此代码将字符串转换为时间
try
{
string hour = stringFormat.Substring(0, 2);
string min = stringFormat.Substring(2, 2);
DateTime dt = new DateTime();
dt = Convert.ToDateTime(hour+":"+min);
return String.Format("{0:t}", dt); ;
}
catch (Exception ex)
{
return "Please Enter Correct format like <0559>";
}
答案 5 :(得分:0)
为什么我的代码很难:
private void timer7_Tick(object sender, EventArgs e)
{
label4.Text = DateTime.Now.TimeOfDay.Hours.ToString() + ":" + DateTime.Now.TimeOfDay.Minutes.ToString() + ":" + DateTime.Now.TimeOfDay.Seconds.ToString() + ":" + DateTime.Now.TimeOfDay.TotalMilliseconds.ToString();
label7.Text = DateTime.Now.Date.Year.ToString() + "-" + DateTime.Now.Date.Month.ToString() + "-" + DateTime.Now.Date.Day.ToString();
}