Rookie Date Question

I have a small database that I want to read, select certain records, and create a text file with them. Each one contains a date field and it is of the Unix Time format - number of seconds from Jan 1, 1970. I am have a horrible time trying to get that date out of the database and into a text format. I thought I had done this at one time a few weeks ago, but due to pilot error I managed to loose my source code and am now trying to re-create it. Can someone give me a few pointers on how to deal with these date fields?

Thanks, Jim

I’m no expert, but maybe this will help:$SecondsFrom1970

Did you try a google quest ?

Convert unix date to YYYY-MM-DD

New Framework :frowning:

There’s Date.TotalSeconds, but that’s from Jan 1, 1904 so you would need to know the number of seconds from Jan 1 1904 until Jan 1 1970

2,082,844,800 seconds

Have the database do the conversion for you and return ANSI format (YYYY-MM-DD).

ISO 8601

Create a new Date object for 1/1/1970 00:00:00 GMT and then add the seconds to it:

  Dim timestamp As Integer 
  Dim d As New Date(1970, 1, 1, 0, 0, 0, 0.0)
  d.TotalSeconds = d.TotalSeconds + timestamp
  Dim s As String = d.SQLDateTime

Or with the new framework:

Dim timestamp As Integer
Dim gmt As New Xojo.Core.TimeZone(0)
Dim d As New Xojo.Core.Date(timestamp, gmt)
Dim s As Text = d.ToText

Guys - I guess I didn’t explain it correctly. The database I’m working with has the date stored as a number of seconds from 1/1/1970. I need to convert that to a format.

I FIGURED IT OUT. I was overthinking it, I believe. All I needed to do was to do thus:

Dim mySeconds as Double
dim myDate as new Date(mySeconds, timezone.Current)

The variable myDate now contains the whole shebang - years, months, days, hours, etc…

Thanks for the input - all working now!


Exactly what Alberto said yesterday.

[quote=386390:@Alberto De Poo]I’m no expert, but maybe this will help:$SecondsFrom1970[/quote]

Dave, almost, but not quite. The big flaw in what I was doing was not defining my DB field as Double. The documentation examples were helpful, but the light bulb didn’t go on until I realized that the DB field was a Double, and not a date. My bad. I kept getting hung up trying to call things Xojo.Code.Date when I didn’t need to. Anyway, I have what I need.

Thanks again, Jim