Convert To Unix TimeStamp







'VB6
Private Function ConvertToUnixTimeStamp(input_datetime) As Long
  Dim d As Date
  d = CDate(input_datetime)
  ConvertToUnixTimeStamp = CStr(DateDiff("s", "01/01/1970 00:00:00", d))
End Function

Private Sub Form_Load()
  Dim dd As Date
  Dim UnixTimeStamp As Long
  dd = Now
  UnixTimeStamp = ConvertToUnixTimeStamp(Now)
  dd = ConvertUnixTimeStampToDateTime(UnixTimeStamp)

  'other way with jscript
  Dim CurrentUnix as String
  dd = Now
  CurrentUnix = GetCurrentUnix(dd)

End Sub

Private Function ConvertUnixTimeStampToDateTime(input_unix_timestamp) As Date
  ConvertUnixTimeStampToDateTime = CStr(Format(DateAdd("s", input_unix_timestamp, "01/01/1970 00:00:00"), "mm/dd/yyyy"))
End Function

Private Function GetCurrentUnix(ByVal t As Double) As String
    With CreateObject("htmlfile")
        .parentWindow.execScript "function GetTimeStamp(t){return new Date(t).getTime() / 1000}", "jscript"
        GetCurrentUnix = .parentWindow.GetTimeStamp(Now)
    End With
End Function


'VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
  Dim uTime As Double
  'Convert to Unix TimeStamp
  uTime = (DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds
  Dim dd As Date
  dd = UnixToDateTime(uTime)

  'other way with jscript
  Dim CurrentUnix As String
  dd = Now
  CurrentUnix = GetCurrentUnix2(dd.ToOADate)
End Sub

Public Function UnixToDateTime(ByVal strUnixTime As Double) As DateTime
  Dim nTimestamp As Double = strUnixTime
  Dim nDateTime As System.DateTime = New System.DateTime(1970, 1, 1, 0, 0, 0, 0)
  nDateTime = nDateTime.AddSeconds(nTimestamp)
  Return nDateTime
End Function

Private Function GetCurrentUnix2(ByVal t As Double) As String
    With CreateObject("htmlfile")
        .parentWindow.execScript("function GetTimeStamp(t){return new Date(t).getTime() / 1000}", "jscript")
        GetCurrentUnix = .parentWindow.GetTimeStamp(Now)
    End With
End Function



Terminal