将2个字符串转换为日期时间 [英] Convert 2 strings into a datetime

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

问题描述

我有一个文本框,其日期和时间格式如下:



2016年8月12日@ 15:00



我需要删除@,然后将结果转换为日期时间字符串。什么是适当的程序,因为日期时间转换总是扔我?



------



另外我有第二个区域,我有一个DateTimePicker(没有时间)和第二个文本框只有一段时间。



EX:dpShipArriveDate = 12月8日2016 / tbShipArriveTime = 15:28

以上项目写入我数据库中的2个独立数据列,需要保持这样。



I需要将这两个项连接为一个日期时间字符串。目前tbShipArriveDate将日期和00:00:00放在一起,我需要将时间从tbShipArriveTime放入原来的tbShipArriveDate并替换08/12/2016 00:00:00以最终得到新值08/12 / 2016 15:28:00这些值必须在24小时内,并且只需要HH:mm。



我的最终目标是比较两个单独的日期时间值并确定result2是否为AFTER(或后来)比结果1。



我尝试了什么:



< pre lang =vb> 私人 Sub tb01Time_TextChanged(发件人正如 对象,e As EventArgs)句柄 tb01Time.TextChanged
Dim result1 As String = tb01Time.ToString
Dim result2 As DateTime = DateTime。解析(dpShipArriveDate.ToString + + tbShipArriveTime.ToString)

Dim late As String

如果 result2> result1 然后
late = True
否则
late = 错误
结束 如果

选择 案例 late.ToString
案例 = True:tb01Time.BackColor = Color.Red
tbShipArriveTime.BackColor = Color.Red
tb01Time.ForeColor = Color.White
tbShipArriveTime.ForeColor = Color.White
Case Else :tb01Time.BackColor = Color.White
tbShipArriveTi me.ForeColor = Color.White
tb01Time.ForeColor = Color.Black
tbShipArriveTime.ForeColor = Color.Black
End 选择
结束 Sub

解决方案

首先,请阅读我对该问题的评论。



你必须将两个字符串转换为单个日期,然后您才能将它与另一个日期进行比较。例如:

  Dim  sTime  As  字符串 =   15:00 
Dim sDate As String = 2016年8月12日
Dim sDateFormat 作为 字符串 = dd MMM yyyy HH:mm
Dim sDateString = String .Concat(sDate, ,sTime)
Dim resultDate 作为 日期

如果 日期 .TryParseExact(sDateString,sDateFormat,Globalization.CultureInfo.InvariantCulture,Globalization.DateTimeStyles.None, resultDate)然后
Console.WriteLine( '{0}'成功转换为日期!,sDateString)
' 你的逻辑比较日期!
其他
Console.WriteLine( < span class =code-string>日期不正确:'{0}',sDateString)
结束 如果





以上代码返回:

'2016年8月12日15:00'成功转换为日期!



欲了解更多详情,请参阅:

DateTime.TryParseExact Method(String,String,IFormatProvider,DateTimeStyles,DateTime)(系统) [ ^ ]

DateTime.Compare方法(DateTime,DateTime)(系统) [ ^ ]

DateTime.CompareTo Method(DateTime)(System) [< a href =https://msdn.microsoft.com/en-us/library/5ata5aya(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]


I have a text box that has a date and time in it formatted as such:

12 Aug 2016 @ 15:00

I need to drop the " @ " and then convert the result to a datetime string. What is proper procedure for this as datetime conversions always throw me?

------

Additionally I have a second area where I have a DateTimePicker (no time) and a 2nd text box with just a time in it.

EX: dpShipArriveDate = 12 Aug 2016 / tbShipArriveTime = 15:28
The above items write to 2 separate data columns in my database and needs to remain so.

I need to concatenate the two items as one datetime string. Currently the tbShipArriveDate puts the date and 00:00:00 and I need to put the time from tbShipArriveTime into the original tbShipArriveDate and replace the 08/12/2016 00:00:00 to end up with a new value of 08/12/2016 15:28:00. The values must be in 24hr time and only HH:mm is required.

My end goal is to compare the two separate datetime values and determine if result2 is AFTER (or later) than result1.

What I have tried:

Private Sub tb01Time_TextChanged(sender As Object, e As EventArgs) Handles tb01Time.TextChanged
        Dim result1 As String = tb01Time.ToString
        Dim result2 As DateTime = DateTime.Parse(dpShipArriveDate.ToString + " " + tbShipArriveTime.ToString)

        Dim late As String

        If result2 > result1 Then
            late = "True"
        Else
            late = "False"
        End If

        Select Case late.ToString
            Case = "True" : tb01Time.BackColor = Color.Red
                tbShipArriveTime.BackColor = Color.Red
                tb01Time.ForeColor = Color.White
                tbShipArriveTime.ForeColor = Color.White
            Case Else : tb01Time.BackColor = Color.White
                tbShipArriveTime.ForeColor = Color.White
                tb01Time.ForeColor = Color.Black
                tbShipArriveTime.ForeColor = Color.Black
        End Select
    End Sub

解决方案

First of all, please read my comment to the question.

You have to convert both strings into single date, then you'll be able to compare it to another date. For example:

Dim sTime As String = "15:00"
Dim sDate As String = "12 Aug 2016"
Dim sDateFormat As String = "dd MMM yyyy HH:mm"
Dim sDateString = String.Concat(sDate, " ", sTime)
Dim resultDate As Date

If Date.TryParseExact(sDateString, sDateFormat, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, resultDate) Then
    Console.WriteLine("'{0}' sucessfuly converted into date!", sDateString)
    'your logic to compare dates!
Else
    Console.WriteLine("Incorrect date: '{0}'", sDateString)
End If



Above code returns:
'12 Aug 2016 15:00' sucessfuly converted into date!

For further details, please see:
DateTime.TryParseExact Method (String, String, IFormatProvider, DateTimeStyles, DateTime) (System)[^]
DateTime.Compare Method (DateTime, DateTime) (System)[^]
DateTime.CompareTo Method (DateTime) (System)[^]


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

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