Recording first run date

I’m having problems working with databases and files.

I want my app to remember the exact date it was first run on a computer.
So it can keep an exact record of how many days it’s been running on that computer.
What’s the best way to do it?

I just want to keep track of two properties: Year, and DayOfYear

I want to do that just for calculating and displaying statistics for the user,
but also I would like to know the safe way to use it for making time trial version apps.

On first run, create a new date object

Dim d as new date

Get the total seconds and save it.

Dim savedTime As Double = d.totalSeconds

Save this variable to a database or file as you wish

Upon subsequent runs, read your saved var., get a new date object to reflect the current time, and perform the necessary divisions to get hours, days, etc.

The problem is I don’t know how to save the total elapsed seconds to a file or database,
and also how to update it after every run.

[quote=119123:@Radium Radiovich]The problem is I don’t know how to save the total elapsed seconds to a file or database,
and also how to update it after every run.[/quote]

Start by practicing with files and databases, and worry about recording first run when you know.

Good example how to store ‘anything’ like first start of your app:
http://www.xojo.com/blog/en/2014/01/saving-preferences.php
or
https://code.google.com/p/realeasyprefs/

Don’t save the total elapsed seconds, just save the initial run’s totalSeconds then calc elapsed seconds when you need them. Trying to save elapsed secs. Every time is way too much work.
The others have given you good references on reading and writing files.

How to use an SQL Database video :slight_smile:

Thanks everyone.
I realized how to save and read files very well now.
This guide especially helped me so much:
http://www.xojo.com/blog/en/2014/01/saving-preferences.php

[quote=120153:@Radium Radiovich]Thanks everyone.
I realized how to save and read files very well now.
This guide especially helped me so much:
http://www.xojo.com/blog/en/2014/01/saving-preferences.php[/quote]

Congratulations ! Now that you are comfortable with loading and saving files, here is how you do the first run thing.

  • In your program, create a FolderItem f which points to your firstrun file
  • if f.exists = false, then create your first run file by saving to it the value of new date.totalseconds
  • if f.exists = true then it means the program was already run once. To get the date for the first run,

dim d as new date d.TotalSeconds = tts //The value you had saved msgbox d.LongDate

You can get the year of first run by using d.year and days by d.day

I noticed I can’t save a value with date datatype into the JSON file. it gives me an error.
How can I solve that?
Currently to MASK the problem, I proceeded to save any part of the date I needed as separate strings into the JSON.
like:
d.year
d.DayOfYear
d.month
etc…

[quote=120319:@Radium Radiovich]I noticed I can’t save a value with date datatype into the JSON file. it gives me an error.
How can I solve that?
Currently to MASK the problem, I proceeded to save any part of the date I needed as separate strings into the JSON.
like:
d.year
d.DayOfYear
d.month
etc…[/quote]

d.year, d.DayOftheYear and d.Month are all integers. You should record them as str(value) to make them strings before you add them to your Json.

Yes I know, I don’t have any problem saving those.
I meant how can I save the whole Date object directly into the JSON?
So later I can load it, and make use of any of its properties (d.year, d.shortdate, d.longdate, etc) that I like?
Is it possible to save the “date object” itself into a file or database?

[quote=120325:@Radium Radiovich]Yes I know, I don’t have any problem saving those.
I meant how can I save the whole Date object directly into the JSON?
So later I can load it, and make use of any of its properties (d.year, d.shortdate, d.longdate, etc) that I like?
Is it possible to save the “date object” itself into a file or database?[/quote]

Saving the TotalSeconds value and then after reloading assigning it to date.Totalseconds, as I posted above, sets the date object to the date you saved it. The Date object uses TotalSeconds as the core of its data ; all properties are derived from it.

Got it!
Thanks