VB.Net学习笔记之日期格式工具类

日期格式工具类

Imports System.IO
Imports System.Runtime.Serialization.Json
Imports System.Text
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq


Public Class TestUtil

	Public Shared Function DateFormatUtil(ByRef dateStr As String, ByRef DateFormat As String) As String
        Try
            If dateStr <> "" Then
                '截取日期前四位查看是/ 还是 - 
                Dim dataSplit As String = dateStr.Substring(4, 1)
                If dataSplit = "/" Or dataSplit = "-" Then
                    Return Convert.ToDateTime(dateStr).ToString(DateFormat)
                Else
                    '判断日期格式是 20220903
                    Dim EndTime As String = CDate(Format(CInt(dateStr), "0000-00-00"))
                    Return Convert.ToDateTime(EndTime).ToString(DateFormat)
                End If
            Else
                Return ""
            End If
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function
    
End Class

以上代码分两个模块 一是 正常的日期格式 2022-08-03 、2022/08/03 这一类日期格式使用Convert.ToDateTime去转化成需要的类型,二是特殊的日期格式 20220803 使用else方法然后进行转化

测试用例

   Private Sub TestData()
        Dim d1 As String = "20220904"
        Dim d2 As String = "2022-09-04"
        Dim d3 As String = "2022/09/04"
        Dim d4 As String = "2022/9/4"
        Dim d5 As String = "2022/09/04 21:40:21"
        Dim ret As String = TestUtil.DateFormatUtil(d1, "yyyy-MM-dd")
    End Sub

以上是string类型格式的日期进行日期转化

#获取系统当前时间

Dim nowDate As String = Format(Now, "yyyy-MM-dd")
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

依楼听疯語丶

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值