Convert DateTime to Unixdatetime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip Wagenaar

    Convert DateTime to Unixdatetime

    I have some code to convert a date to the unixdatetime representation (number
    of seconds since 1970). Only problem is that it only counts from the whole
    day, it ignores the times part. So convert 1/1/1970 and 1/1/1970 8:00:00 AM
    both return 0 but the last should return 28800.

    My code:
    Try
    Dim dte As DateTime
    dte = DateTime.Parse( CmdArgs(1))
    Console.WriteLi ne(DateDiff(Dat eInterval.Secon d,
    #1/1/1970#, dte))
    Catch ex As Exception
    Console.WriteLi ne(ex.Message)
    End Try

    When I try to add 12:00:00 AM to the date in the DateDiff funcion vs.net
    removes that.

    Any ideas?

  • Cor Ligthert [MVP]

    #2
    Re: Convert DateTime to Unixdatetime

    Phil,

    I tested again the method I showed you in your previous question about that.

    It shows 28800 with the values you show now.

    I hope this helps,

    Cor


    Comment

    • Armin Zingler

      #3
      Re: Convert DateTime to Unixdatetime

      "Philip Wagenaar" <philip.wagenaa r@online.nospam > schrieb[color=blue]
      > I have some code to convert a date to the unixdatetime
      > representation (number of seconds since 1970). Only problem is that
      > it only counts from the whole day, it ignores the times part. So
      > convert 1/1/1970 and 1/1/1970 8:00:00 AM both return 0 but the last
      > should return 28800.
      >
      > My code:
      > Try
      > Dim dte As DateTime
      > dte = DateTime.Parse( CmdArgs(1))[/color]

      What *exactly* does cmdargs(1) contain?
      [color=blue]
      > Console.WriteLi ne(DateDiff(Dat eInterval.Secon d, #1/1/1970#, dte))
      > Catch ex As Exception
      > Console.WriteLi ne(ex.Message)
      > End Try
      >
      > When I try to add 12:00:00 AM to the date in the DateDiff funcion
      > vs.net removes that.[/color]

      12:00 AM is midnight. It's *display* is optional. #1/1/1970 12:00 AM# and
      #1/1/1970# are identical values.
      [color=blue]
      > Any ideas?[/color]


      Use the solution I already gave you some days ago:

      dim diff as double

      diff = yourdate.subtra ct(#1/1/1970#).totalsec onds


      Armin

      Comment

      • Philip Wagenaar

        #4
        Re: Convert DateTime to Unixdatetime

        Cor try this with command line app:
        Try
        Dim dte As DateTime
        dte = DateTime.Parse( CmdArgs(1))
        Console.WriteLi ne(CType(dte.Su btract(New
        System.DateTime (1970, 1, 1)), TimeSpan).Total Seconds.ToStrin g())
        Catch ex As Exception
        Console.WriteLi ne(ex.Message)
        End Try

        Seems that conversion of commandline argument to datetime is going wrong.
        Any thoughts on that?

        "Cor Ligthert [MVP]" wrote:
        [color=blue]
        > Phil,
        >
        > I tested again the method I showed you in your previous question about that.
        >
        > It shows 28800 with the values you show now.
        >
        > I hope this helps,
        >
        > Cor
        >
        >
        >[/color]

        Comment

        • Philip Wagenaar

          #5
          Re: Convert DateTime to Unixdatetime

          Armin I run it like this

          ..exe" /converttounixda te 1/1/1970 4:00:00 am

          and it returns 0.

          The code now is:

          Try
          Dim dte As DateTime
          dte = DateTime.Parse( CmdArgs(1))
          Console.WriteLi ne(CType(dte.Su btract(New
          System.DateTime (1970, 1, 1)), TimeSpan).Total Seconds.ToStrin g())
          Catch ex As Exception
          Console.WriteLi ne(ex.Message)
          End Try


          "Armin Zingler" wrote:
          [color=blue]
          > "Philip Wagenaar" <philip.wagenaa r@online.nospam > schrieb[color=green]
          > > I have some code to convert a date to the unixdatetime
          > > representation (number of seconds since 1970). Only problem is that
          > > it only counts from the whole day, it ignores the times part. So
          > > convert 1/1/1970 and 1/1/1970 8:00:00 AM both return 0 but the last
          > > should return 28800.
          > >
          > > My code:
          > > Try
          > > Dim dte As DateTime
          > > dte = DateTime.Parse( CmdArgs(1))[/color]
          >
          > What *exactly* does cmdargs(1) contain?
          >[color=green]
          > > Console.WriteLi ne(DateDiff(Dat eInterval.Secon d, #1/1/1970#, dte))
          > > Catch ex As Exception
          > > Console.WriteLi ne(ex.Message)
          > > End Try
          > >
          > > When I try to add 12:00:00 AM to the date in the DateDiff funcion
          > > vs.net removes that.[/color]
          >
          > 12:00 AM is midnight. It's *display* is optional. #1/1/1970 12:00 AM# and
          > #1/1/1970# are identical values.
          >[color=green]
          > > Any ideas?[/color]
          >
          >
          > Use the solution I already gave you some days ago:
          >
          > dim diff as double
          >
          > diff = yourdate.subtra ct(#1/1/1970#).totalsec onds
          >
          >
          > Armin
          >
          >[/color]

          Comment

          • Peter Huang [MSFT]

            #6
            Re: Convert DateTime to Unixdatetime

            Hi

            I think you may try to enbrace the 1/1/1970 4:00:00 am into a "" , as
            "1/1/1970 4:00:00 am".

            Or it will be broken into three strings 1/1/1970, 4:00:00 ,am in the
            Environment.Get CommandLineArgs ().

            Best regards,

            Peter Huang
            Microsoft Online Partner Support

            Get Secure! - www.microsoft.com/security
            This posting is provided "AS IS" with no warranties, and confers no rights.

            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Convert DateTime to Unixdatetime

              Philip,

              That is not the code I gave you.

              Translated to this problem it is this

              MessageBox.Show (CType(New Date(1970, 1, 1, 8, 0, 0).Subtract(New
              System.DateTime (1970, 1, 1)), TimeSpan).Total Seconds.ToStrin g)

              Or broken up
              dim dt8Oclock as datetime = New Date(1970, 1, 1, 8, 0, 0)
              dim dt0Oclock as datetiem = New Date(1970, 1, 1)

              Messagebox.Show (CType(dt8Ocloc k).Subtract(dt0 Oclock)),TimeSp an).TotalSecond s.ToString

              I use the ISO times because that is in every culture the same.

              I hope this helps,

              Cor


              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Convert DateTime to Unixdatetime

                Philip,

                I was looking at the wrong code, so it is the code I gave you, however try
                my sample that I added at the wrong place.

                Translated to this problem it is this

                MessageBox.Show (CType(New Date(1970, 1, 1, 8, 0, 0).Subtract(New
                System.DateTime (1970, 1, 1)), TimeSpan).Total Seconds.ToStrin g)

                I use the ISO times because that is in every culture the same.

                And than to set your date to a string try for that
                dte = CDate(CmdArgs(1 ))

                My expirience is that using CDate helps to overcome a lot of problems.

                I hope this helps,

                If not reply than I try a test with setting the culture first (tell than
                what that is).

                Cor


                "Philip Wagenaar" <philip.wagenaa r@online.nospam > schreef in bericht
                news:BFEDDC65-691F-46F9-B37C-B5807ACEB2A3@mi crosoft.com...[color=blue]
                > Cor try this with command line app:
                > Try
                > Dim dte As DateTime
                > dte = DateTime.Parse( CmdArgs(1))
                > Console.WriteLi ne(CType(dte.Su btract(New
                > System.DateTime (1970, 1, 1)), TimeSpan).Total Seconds.ToStrin g())
                > Catch ex As Exception
                > Console.WriteLi ne(ex.Message)
                > End Try
                >
                > Seems that conversion of commandline argument to datetime is going wrong.
                > Any thoughts on that?
                >
                > "Cor Ligthert [MVP]" wrote:
                >[color=green]
                >> Phil,
                >>
                >> I tested again the method I showed you in your previous question about
                >> that.
                >>
                >> It shows 28800 with the values you show now.
                >>
                >> I hope this helps,
                >>
                >> Cor
                >>
                >>
                >>[/color][/color]


                Comment

                • Philip Wagenaar

                  #9
                  Re: Convert DateTime to Unixdatetime

                  That did the trick!

                  thank you.

                  ""Peter Huang" [MSFT]" wrote:
                  [color=blue]
                  > Hi
                  >
                  > I think you may try to enbrace the 1/1/1970 4:00:00 am into a "" , as
                  > "1/1/1970 4:00:00 am".
                  >
                  > Or it will be broken into three strings 1/1/1970, 4:00:00 ,am in the
                  > Environment.Get CommandLineArgs ().
                  >
                  > Best regards,
                  >
                  > Peter Huang
                  > Microsoft Online Partner Support
                  >
                  > Get Secure! - www.microsoft.com/security
                  > This posting is provided "AS IS" with no warranties, and confers no rights.
                  >
                  >[/color]

                  Comment

                  • Armin Zingler

                    #10
                    Re: Convert DateTime to Unixdatetime

                    "Philip Wagenaar" <philip.wagenaa r@online.nospam > schrieb[color=blue]
                    > Armin I run it like this
                    >
                    > .exe" /converttounixda te 1/1/1970 4:00:00 am
                    >
                    > and it returns 0.[/color]


                    Arguments are separated by blanks, thus the 2nd arg in CmdArgs(1) is
                    "1/1/1970" only.



                    Armin

                    Comment

                    • Peter Huang [MSFT]

                      #11
                      Re: Convert DateTime to Unixdatetime

                      Hi

                      You are welcomed!

                      Best regards,

                      Peter Huang
                      Microsoft Online Partner Support

                      Get Secure! - www.microsoft.com/security
                      This posting is provided "AS IS" with no warranties, and confers no rights.

                      Comment

                      Working...