Results 1 to 6 of 6

Thread: [RESOLVED] convert date timespan with regional time

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,606

    Resolved [RESOLVED] convert date timespan with regional time

    Hi.
    I have a time string this: /Date(1644012000000+0200)/

    Now I have 2 issue
    1)If i remove the extra time I get a wrong time doing this:

    Code:
            Dim dtest As DateTime = "1/1/1970"     
            dtest = dtest + TimeSpan.FromMilliseconds(Convert.ToDouble(strvistadate))
    Mind you I'm removing the strings and leave only the number portion (1644012000000) or if I add the + 0200 : 1644012000200

    This is supposed to give out 2022-02-05 but it's giving 2022-02-04 . So I'm missing a day.
    I haven't dealt with regional addition so I'm not sure how would I use the extra +0200 .

    Even with a datetimeoffset it will not work:

    Code:
       Dim dtest As DateTimeOffset = "1/1/1970"
       dtest = dtest + TimeSpan.FromMilliseconds(Convert.ToInt64("1644012000200"))
      Dim correctdate As Date = dtest.Date.ToString
    So I guess somehow I need to add the extra +0200 but it's not an just an addition on the timestamp, it's something else.
    Thanks.


    EDIT:

    Best bet I found so far is get the +0200 , treat it as an additional hour.

    dtest = dtest + TimeSpan.FromMilliseconds(Convert.ToDouble("1644012000200")) + TimeSpan.FromHours(2)

    That seems lame. Isn't there something to detect the hours or I should just put a detection mechanism in a function?

    Thanks
    Last edited by sapator; Dec 7th, 2021 at 11:08 AM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: convert date timespan with regional time

    This produces 2/5/2022

    Code:
            Dim dtest As DateTime = #1/1/1970#
    
            dtest = dtest + TimeSpan.FromMilliseconds(1644012000000) + TimeSpan.FromHours(2)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: convert date timespan with regional time

    The +200 part could be written as +2:00 (ie: 2 hours and 0 minutes), I'm don't know why the standard is without the colon.

    Note that some time zones aren't exact hours (eg: +0030 is possible), so you do need to detect both parts and add them as time.

    A separate function to do that is probably wise.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: convert date timespan with regional time

    if I add the + 0200 : 1644012000200
    Yeah, stop doing that... the +0200 shows the hour offset from UTC, typically representing the timezone. For example I'm in the Eastern TZ, which is UTC-5 (or-4 depending on the time of year & DST) In this case it's UTC +2 so simply adding 200 to your milliseconds isn't the answer.

    TimeSpan.FromMilliseconds(Convert.ToDouble("1644012000200")) + TimeSpan.FromHours(2)
    That's closer ... but it should still only affect the hours portion of the dateTime, not the Date ... you should be coming out with the correct date at least (I think). Unless because it's +2 you need to add a day. ????
    Honestly I'm not 100% sure...

    Here's an SO thread where someone had a similar problem ... and there's a solution

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: convert date timespan with regional time

    Quote Originally Posted by techgnome View Post
    Yeah, stop doing that... the +0200 shows the hour offset from UTC, typically representing the timezone. For example I'm in the Eastern TZ, which is UTC-5 (or-4 depending on the time of year & DST) In this case it's UTC +2 so simply adding 200 to your milliseconds isn't the answer.


    That's closer ... but it should still only affect the hours portion of the dateTime, not the Date ... you should be coming out with the correct date at least (I think). Unless because it's +2 you need to add a day. ????
    Honestly I'm not 100% sure...

    Here's an SO thread where someone had a similar problem ... and there's a solution

    -tg
    Quote Originally Posted by si_the_geek View Post
    The +200 part could be written as +2:00 (ie: 2 hours and 0 minutes), I'm don't know why the standard is without the colon.

    Note that some time zones aren't exact hours (eg: +0030 is possible), so you do need to detect both parts and add them as time.

    A separate function to do that is probably wise.
    I think we all agree that this isn't a standard format. My understanding is that Unix timestamps are time zone independent, they are by definition UTC times.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,606

    Re: convert date timespan with regional time

    I will have to ask the company I get the data from if they have this format.
    All those years they did not gave out a region time but suddenly they do.
    Thank you all.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width