Search code examples
javascriptalasql

How do you create a time field in AlaSQL


I am trying to make a simple app to analyze how I spend my time. To do this I want to make a SQL database using AlaSQL with the following schema:

id - some unique value
date - a date field
start_time - a time field
end_time - a time field
task - a string
category - a string that groups tasks

I am not quite sure how to make the date and time fields... can anyone help me with this?


Solution

  • There are two methods to use date/time fields in AlaSQL:

    1. Use DATE and DATETIME data types. In this case AlaSQL stores data in text fotmat, like: 'YYYYMMDD' and 'YYYYMMDDHHMMSS'

    2. Another options is to use Date (with first upper case "D" and other lower case letters) data type. In this case AlaSQL create standard JavaScript Date object.

    Currently, there is no special format for TIME data type.

    So, you can model your data model like below:

    CREATE TABLE rec (
        date DATE,
        start_time DATETIME,
        end_time DATETIME
    );
    

    or

    CREATE TABLE rec (
        start_time Date,
        end_time Date
    );
    

    Currently there is no TIMEDIFF() function in AlaSQL, but we can add it by your request.

    (disclaimer: I am the author of AlaSQL)