C# 如何修复错误的格式时间?
我在插入有关时间的数据时发现了一个问题 通常,用户将从测试仪保存CSV文件到服务器上的我 我将按程序读取和插入数据 我对列时间有问题,因为没有符号“:”表示小时、分钟和秒 示例:231620 我用子串函数修正C# 如何修复错误的格式时间?,c#,C#,我在插入有关时间的数据时发现了一个问题 通常,用户将从测试仪保存CSV文件到服务器上的我 我将按程序读取和插入数据 我对列时间有问题,因为没有符号“:”表示小时、分钟和秒 示例:231620 我用子串函数修正 string start_InspHour_Converted = start_InspHour.Substring(0, 2) + ":" + start_InspHour.Substring(2, 2) + ":" + s
string start_InspHour_Converted =
start_InspHour.Substring(0, 2) + ":" +
start_InspHour.Substring(2, 2) + ":" +
start_InspHour.Substring(4, 2);
但有些文件格式错误
示例:22411
02:24:11应该是022411
我认为专栏时间必须将格式改为文本
我不知道为什么用户不能修复它,他们什么也不做,仍然给我发送更多的文件
我需要帮助来修复这个编码,但我不知道关键字搜索
对不起,我的英语你能不能有一个这样简单的条件
if (start_InspHour.Length == 6)
{
string start_InspHour_Converted = start_InspHour.Substring(0, 2) + ":" +
start_InspHour.Substring(2, 2) + ":" + start_InspHour.Substring(4, 2);
}
else
{
string start_InspHour_Converted = "0" + start_InspHour.Substring(0, 1) + ":" +
start_InspHour.Substring(1, 2) + ":" + start_InspHour.Substring(3, 2);
}
你能有一个简单的条件吗
if (start_InspHour.Length == 6)
{
string start_InspHour_Converted = start_InspHour.Substring(0, 2) + ":" +
start_InspHour.Substring(2, 2) + ":" + start_InspHour.Substring(4, 2);
}
else
{
string start_InspHour_Converted = "0" + start_InspHour.Substring(0, 1) + ":" +
start_InspHour.Substring(1, 2) + ":" + start_InspHour.Substring(3, 2);
}
定义一个类TimeStr
class TimeStr {
// private data members.
private int _hours;
private int _minutes;
private int _seconds;
// public accessor methods.
public int Hours => _hours;
public int Minutes => _minutes;
public int Seconds => _seconds;
// class constructor.
public TimeStr(string strIn) {
// HH:MM:SS
Match m = Regex.Match(strIn, @"^(\d{2}):(\d{2}):(\d{2})$");
if (m.Success) {
this._hours = int.Parse(m.Groups[1].Value);
this._minutes = int.Parse(m.Groups[2].Value);
this._seconds = int.Parse(m.Groups[3].Value);
} else {
// HHMMSS
m = Regex.Match(strIn, @"^(\d{2})(\d{2})(\d{2})$");
if (m.Success) {
this._hours = int.Parse(m.Groups[1].Value);
this._minutes = int.Parse(m.Groups[2].Value);
this._seconds = int.Parse(m.Groups[3].Value);
} else {
// HMMSS
m = Regex.Match(strIn, @"^(\d)(\d{2})(\d{2})$");
if (m.Success) {
this._hours = int.Parse(m.Groups[1].Value);
this._minutes = int.Parse(m.Groups[2].Value);
this._seconds = int.Parse(m.Groups[3].Value);
} else {
// Unrecognized format.
throw new ArgumentException();
}
}
}
}
// returns time as HH:MM:SS
public override string ToString() {
return $"{this._hours.ToString("00")}:{this._minutes.ToString("00")}:{this._seconds.ToString("00")}";
}
这就是如何使用它
class Program
{
public static void Main(String[] args) {
TimeStr t1 = new Time("23:16:20");
Console.WriteLine(t1.ToString());
TimeStr t2 = new Time("231620");
Console.WriteLine(t2.ToString());
TimeStr t3 = new Time("31620");
Console.WriteLine(t3.ToString());
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
}
定义一个类TimeStr
class TimeStr {
// private data members.
private int _hours;
private int _minutes;
private int _seconds;
// public accessor methods.
public int Hours => _hours;
public int Minutes => _minutes;
public int Seconds => _seconds;
// class constructor.
public TimeStr(string strIn) {
// HH:MM:SS
Match m = Regex.Match(strIn, @"^(\d{2}):(\d{2}):(\d{2})$");
if (m.Success) {
this._hours = int.Parse(m.Groups[1].Value);
this._minutes = int.Parse(m.Groups[2].Value);
this._seconds = int.Parse(m.Groups[3].Value);
} else {
// HHMMSS
m = Regex.Match(strIn, @"^(\d{2})(\d{2})(\d{2})$");
if (m.Success) {
this._hours = int.Parse(m.Groups[1].Value);
this._minutes = int.Parse(m.Groups[2].Value);
this._seconds = int.Parse(m.Groups[3].Value);
} else {
// HMMSS
m = Regex.Match(strIn, @"^(\d)(\d{2})(\d{2})$");
if (m.Success) {
this._hours = int.Parse(m.Groups[1].Value);
this._minutes = int.Parse(m.Groups[2].Value);
this._seconds = int.Parse(m.Groups[3].Value);
} else {
// Unrecognized format.
throw new ArgumentException();
}
}
}
}
// returns time as HH:MM:SS
public override string ToString() {
return $"{this._hours.ToString("00")}:{this._minutes.ToString("00")}:{this._seconds.ToString("00")}";
}
这就是如何使用它
class Program
{
public static void Main(String[] args) {
TimeStr t1 = new Time("23:16:20");
Console.WriteLine(t1.ToString());
TimeStr t2 = new Time("231620");
Console.WriteLine(t2.ToString());
TimeStr t3 = new Time("31620");
Console.WriteLine(t3.ToString());
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
}
强制链接。。。如果您不能强制执行正确的ISO8601输入。。。请回答以下问题,以澄清您对“1234”有效输入的预期结果(这也是您不应该尝试的一个很好的例子)……让用户以适当的时间格式保存数据是否更容易?
var dtm=DateTime.ParseExact(start_InspHour.PadLeft(6,'0'),“HHmmss”,null)代码>,如果用户不愿意的话。字符串输出=null;if(DateTime.TryParseExact(input.PadLeft(6,'0'),“HHmmss”,CultureInfo.InvariantCulture,DateTimeStyles.None,out DateTime result))output=result.ToString(“HH:mm:ss”)代码>您可能想考虑这一点…如果“您的”代码从一个畸形的字符串中确定了一个时间值,其中存在歧义,并且“您的”代码错误地确定了一个时间,并且用户受到了这个影响……然后,用户将责怪“您的”代码。让您的生活更轻松,并将适当的时间格式要求强加给用户,这样他们就不会因为错误的时间而责怪任何人,只能责怪自己。只是一个想法。强制链接。。。如果您不能强制执行正确的ISO8601输入。。。请回答以下问题,以澄清您对“1234”有效输入的预期结果(这也是您不应该尝试的一个很好的例子)……让用户以适当的时间格式保存数据是否更容易?var dtm=DateTime.ParseExact(start_InspHour.PadLeft(6,'0'),“HHmmss”,null)代码>,如果用户不愿意的话。字符串输出=null;if(DateTime.TryParseExact(input.PadLeft(6,'0'),“HHmmss”,CultureInfo.InvariantCulture,DateTimeStyles.None,out DateTime result))output=result.ToString(“HH:mm:ss”)代码>您可能想考虑这一点…如果“您的”代码从一个畸形的字符串中确定了一个时间值,其中存在歧义,并且“您的”代码错误地确定了一个时间,并且用户受到了这个影响……然后,用户将责怪“您的”代码。让您的生活更轻松,并将适当的时间格式要求强加给用户,这样他们就不会因为错误的时间而责怪任何人,只能责怪自己。只是一个想法。我的荣幸……我的荣幸。。。。