How do I convert unixtime in readable date format

  • That's what I have right now. The year is correct, but the month isn't. If the date is like may 1º it gets 3. months.

    ->DataV = 2019.05.01

    -> System: Set unixDataV to Browser.ExecJS("new Date('" & DataV & "').getTime() / 1000")

    -> System: Set unixDataCD to Browser.ExecJS("new Date('" & DataV & "').getTime() / 1000") + (21×86400)

    -> Text: Set text to 1970 + floor(unixDataCD÷ano_insec)&"/"&floor((unixDataCD%ano_insec)÷mes_insec)&"/"&floor((unixDataCD%ano_insec)%mes_insec)÷dia_insec

    Thanks.

    Tagged:

  • I 'am sorry I don't really understand what you are doing, but if you want convert Unixtime to a normal format this should be easier:

    -> Browser: Execute javascript "this.date = new Date("&unixtime&");"

    -> System: Set month to Browser.ExecJS("this.date.getMonth()")

    -> System: Set day to Browser.ExecJS("this.date.getDate()")

    -> System: Set year to Browser.ExecJS("this.date.getFullYear()")

    -> Text: Set text to year&"."&month+1&"."&day

  • Hey Asmodean Thanks for answer. I was trying to get a readable date informed by the user, convert it to unixtime, add 21 days by adding (21*86400) to unixtime format and then convert it again to a readable date.

    How could it be done?

    I tried

    -> Browser: Execute javascript "this.date = new Date("&unixtime&"); + (21*86400)"

    But It doesn't work.

  • Create a variable called newUnixTime.

    newUnixTime = unixtime+(21*86400)

    Then repeat Asmodean code with the new variable:

    -> Browser: Execute javascript "this.date = new Date("&newUnixTime&");"

    -> System: Set month to Browser.ExecJS("this.date.getMonth()")

    -> System: Set day to Browser.ExecJS("this.date.getDate()")

    -> System: Set year to Browser.ExecJS("this.date.getFullYear()")

    -> Text: Set text to year&"."&month+1&"."&day

  • Elliott But this way I'm getting the current date, I need to get the date informed by the user, which doesn't mean It'll be the current day.

  • kossglobal now I unterstand what you want. Look of this works for you

    | Global number year‎ = 0

    | Global number month‎ = 0

    | Global number day‎ = 0

    | Global number myUnixtime‎ =0

    | Global string DateV‎ = "2018.4.29"

    | Global number daysToAdd‎ = 21

    + System: On start of layout

    -> System: Set myUnixtime to Browser.ExecJS("Date.parse("&DateV&")")

    -> System: Set myUnixtime to myUnixtime+daysToAdd×86400000

    -> Browser: Execute javascript "this.myDate = new Date("&myUnixtime&")"

    -> System: Set day to Browser.ExecJS("this.myDate.getDate()")

    -> System: Set month to Browser.ExecJS("this.myDate.getMonth()")+1

    -> System: Set year to Browser.ExecJS("this.myDate.getFullYear()")

    -> Text: Set text to year&"."&month&"."&day

  • Asmodean Now it works but replacing the:

    -> System: Set myUnixtime to Browser.ExecJS("Date.parse("&DateV&")")

    with

    -> System: Set myUnixTime to Browser.ExecJS("new Date('" & DateV & "').getTime()")

    Thanks for your time. ^^

  • And I'm having an issue with JS date's which make no sense.

    -> SkillFinished: Set text to Browser.ExecJS("Date("&UDATE&")")

    Doesn't matter what I set UDATE to, it spits out the local date time in HUGE string format.

  • They added an expression: unixtime

    Maybe make a feature suggestion for the rest of the date api?

  • Well, I ended up just doing this.

    function timeConverter(UNIX_timestamp){
     var a = new Date(UNIX_timestamp);
     var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
     var year = a.getFullYear();
     var month = months[a.getMonth()];
     var date = a.getDate();
     var hour = a.getHours();
     var min = a.getMinutes() < 10 ? '0' + a.getMinutes() : a.getMinutes(); 
     var sec = a.getSeconds() < 10 ? '0' + a.getSeconds() : a.getSeconds()
     var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
     return time;
    }

    Good reason for me not to invest a lot of time into learning JS. When the base code doesn't even have basic date/time conversions then it's still not a real language.

    Proof?

    Date.now() - gives the Unix timestamp in UTC.

    Date() - gives the date string in LOCAL time.

    Seriously? What logic came up with that?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • They added an expression: unixtime

    Maybe make a feature suggestion for the rest of the date api?

    That should be built into a language like JS. Instead, what I'm discovering is that in JS you either have to include some huge library or write your own.

    I haven't checked the expression and I may. All I wanted to do was convert a unix time (in the future) to a date time string. I feel like landing on the moon would have been an easier task.

    But... I guess I expect to much from a programming language.

  • I haven't checked the expression and I may. All I wanted to do was convert a unix time (in the future) to a date time string. I feel like landing on the moon would have been an easier task.

    Well they did happen pretty close to each other.

  • Time zones (and daylight savings or the lack of it) are a huge pain in the ass. Leap seconds and leap years are no fun either.

  • Might give a yell to Toby R to get his plug converted:

    construct.net/en/forum/extending-construct-2/addons-29/plugin-trclockparser-124970

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)