• vb.net的日期工具类


    cloneDate复制一个Date对象。接收Date对象,输出该对象的复制。

    Function cloneDate(myDate As Date)
        cloneDate = DateAdd(DateInterval.Day, 0, myDate)
    End Function
    
    • 1
    • 2
    • 3

    返回今天的15点整。

    Function nowAtHour(myhour As Integer)
        Dim mynow As Date
        mynow = Now()
        Dim myyear As Integer
        myyear = Year(mynow)
        Dim mymonth As Integer
        mymonth = Month(mynow)
        Dim myday As Integer = Day(mynow)
    
        nowAtHour = New Date(myyear, mymonth, myday, myhour, 0, 0)
    End Function
    Dim myDate As Integer=nowAtHour(15)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    因为Date对象的Second属性是只读的,不可以修改。所以写了设置秒数的方法。

    Function setSecond(mysecond As Integer, mydate As Date)
        Dim myyear As Integer = Year(mydate)
        Dim mymonth As Integer = Month(mydate)
        Dim myday As Integer = Day(mydate)
        Dim myhour As Integer = Hour(mydate)
        Dim myminute As Integer = Minute(mydate)
        setSecond = New Date(myyear, mymonth, myday, myhour, myminute, mysecond)
    End Function
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    设置分钟数

     Function setMinute(myminute As Integer, mydate As Date)
         Dim myyear As Integer = Year(mydate)
         Dim mymonth As Integer = Month(mydate)
         Dim myday As Integer = Day(mydate)
         Dim myhour As Integer = Hour(mydate)
         Dim mysecond As Integer = Second(mydate)
         setMinute = New Date(myyear, mymonth, myday, myhour, myminute, mysecond)
     End Function
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    设置小时数

    Function setHour(myhour As Integer, mydate As Date)
        Dim myyear As Integer = Year(mydate)
        Dim mymonth As Integer = Month(mydate)
        Dim myday As Integer = Day(mydate)
        Dim myminute As Integer = Minute(mydate)
        Dim mysecond As Integer = Second(mydate)
        setHour = New Date(myyear, mymonth, myday, myhour, myminute, mysecond)
    End Function
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    设置天数

      Function setDay(myday As Integer, mydate As Date)
          Dim myyear As Integer = Year(mydate)
          Dim mymonth As Integer = Month(mydate)
    
          Dim myhour As Integer = Hour(mydate)
          Dim myminute As Integer = Minute(mydate)
          Dim mysecond As Integer = Second(mydate)
          setDay = New Date(myyear, mymonth, myday, myhour, myminute, mysecond)
      End Function
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    设置月份

    Function setMonth(mymonth As Integer, mydate As Date)
        Dim myyear As Integer = Year(mydate)
        Dim myday As Integer = Day(mydate)
        Dim myhour As Integer = Hour(mydate)
        Dim myminute As Integer = Minute(mydate)
        Dim mysecond As Integer = Second(mydate)
        setMonth = New Date(myyear, mymonth, myday, myhour, myminute, mysecond)
    End Function
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    设置年份

    Function setYear(myyear As Integer, mydate As Date)
        Dim mymonth As Integer = Month(mydate)
        Dim myday As Integer = Day(mydate)
        Dim myhour As Integer = Hour(mydate)
        Dim myminute As Integer = Minute(mydate)
        Dim mysecond As Integer = Second(mydate)
        setYear = New Date(myyear, mymonth, myday, myhour, myminute, mysecond)
    End Function
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    76. 最小覆盖子串
    Django使用mysqlclient服务连接并写入数据库
    服务端Skynet(一)——源码浅析
    蓝桥杯C/C++省赛:排它平方数
    车路协同 智能路侧设备网络安全接入技术要求
    git提交代码到远程仓库
    AI:76-基于机器学习的智能城市交通管理
    Dockerfile文件自动化生成R4L镜像
    MyBatis 配置与测试方式
    [cmd]SpringBoot项目启动时报错,显示端口已被占用
  • 原文地址:https://blog.csdn.net/zhourongxiang1/article/details/136595347