110,535
社区成员
发帖
与我相关
我的任务
分享
string st1 = "23:00";//开始时间
string st2 = "03:43";//结束时间
DateTime dt1 = Convert.ToDateTime(st1);
DateTime dt2 = Convert.ToDateTime(st2);
//格式化在00:00-23:59的范围
TimeSpan ts;
if (dt1 > dt2)
{
ts = dt2.AddDays(1) - dt1;
}
else
{
ts = dt2 - dt1;
}
string output = String.Format("{0:00}:{1:00}", ts.Hours, ts.Minutes);
MessageBox.Show(output);
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Label1.Text = (String.Format("{1}到{2} : {3}{0}{4}到{5} : {6}{0}{7}到{8} : {9}{0}{10}到{11} : {12}", vbCrLf,
"00:01", "00:34", Demo("00:01", "00:34"),
"01:01", "00:34", Demo("01:01", "00:34"),
"00:34", "00:33", Demo("00:34", "00:33"),
"11:34", "00:33", Demo("11:34", "00:33")
))
End Sub
Private Function Demo(time1 As String, time2 As String) As String
Dim m1 As Match = Regex.Match(time1, "(?<hour>[\d]{1,2})\:(?<minute>[\d]{1,2})", RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)
If m1.Success Then
Dim hour1, minute1 As Integer
hour1 = Integer.Parse(m1.Groups("hour").Value)
minute1 = Integer.Parse(m1.Groups("minute").Value)
m1 = Regex.Match(time2, "(?<hour>[\d]{1,2})\:(?<minute>[\d]{1,2})", RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)
If m1.Success Then
hour1 = Integer.Parse(m1.Groups("hour").Value) - hour1
minute1 = Integer.Parse(m1.Groups("minute").Value) - minute1
If minute1 < 0 Then
minute1 += 60
hour1 -= 1
End If
If hour1 < 0 Then
hour1 += 24
End If
Return String.Format("{0}:{1}", hour1.ToString("D2"), minute1.ToString("D2"))
End If
End If
Return "err"
End Function
End Class
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text.RegularExpressions;
public class Form1
{
private void Form1_Load(object sender, EventArgs e)
{
Label1.Text = (string.Format("{1}到{2} : {3}{0}{4}到{5} : {6}{0}{7}到{8} : {9}{0}{10}到{11} : {12}", Constants.vbCrLf, "00:01", "00:34", Demo("00:01", "00:34"), "01:01", "00:34", Demo("01:01", "00:34"), "00:34", "00:33",
Demo("00:34", "00:33"), "11:34", "00:33", Demo("11:34", "00:33")));
}
private string Demo(string time1, string time2)
{
Match m1 = Regex.Match(time1, "(?<hour>[\\d]{1,2})\\:(?<minute>[\\d]{1,2})", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
if (m1.Success) {
int hour1 = 0;
int minute1 = 0;
hour1 = int.Parse(m1.Groups["hour"].Value);
minute1 = int.Parse(m1.Groups["minute"].Value);
m1 = Regex.Match(time2, "(?<hour>[\\d]{1,2})\\:(?<minute>[\\d]{1,2})", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
if (m1.Success) {
hour1 = int.Parse(m1.Groups["hour"].Value) - hour1;
minute1 = int.Parse(m1.Groups["minute"].Value) - minute1;
if (minute1 < 0) {
minute1 += 60;
hour1 -= 1;
}
if (hour1 < 0) {
hour1 += 24;
}
return string.Format("{0}:{1}", hour1.ToString("D2"), minute1.ToString("D2"));
}
}
return "err";
}
public Form1()
{
Load += Form1_Load;
}
}