Data Management

Converting separate time and date into a timestamp

Hey there! In this guide, we'll show you how to combine separate date and time inputs into a single timestamp using Xano and Bravo Studio. This can be useful when you need to store or send a complete date and time as a timestamp in your application.

Step 1: Set Up Inputs

First, let's set up our inputs. We'll need two separate inputs: one for the date and one for the time. These inputs will come from your Bravo Studio frontend or another source.

In Xano, create two input variables:

  1. Date as Text: This will be the date input, received as text.
  2. Time: This will be the time input, also received as text.

Step 2: Concatenate Date and Time

Next, we'll combine the date and time inputs into a single variable using Xano's data manipulation functions.

  1. Add a new function: Data Manipulation > Create Variable.
  2. Name the new variable human_timestamp.
  3. Select the date input variable.
  4. Add a Concatenate filter.
  5. Select the time input variable as the second input for the concatenate filter.
  6. Set the separator to a single space character.
  7. Check, update, and save the function.

Now you should have a human_timestamp variable that combines the date and time with a space in between.

Step 3: Parse the Timestamp

Next, we'll parse the human_timestamp into a Unix timestamp using Xano's parse_timestamp filter.

  1. Add a new function: Data Manipulation > Create Variable.
  2. Name the new variable new_timestamp.
  3. Select the human_timestamp variable.
  4. Add a parse_timestamp filter.

Here's where the magic happens! We need to provide the format of our human_timestamp string so that Xano can parse it correctly.

Xano uses PHP's date and time formatting syntax under the hood. You can find the complete list of format characters in the Xano documentation.

For our example date format of "May 4, 2022 4:30 PM", the format string would be:

F j, Y g:i A

Here's what each part means:

  • F: Full textual representation of the month (e.g., January)
  • j: Day of the month without leading zeros (1 to 31)
  • ,: Literal comma separator
  • Y: Full numeric representation of the year (4 digits)
  • g: 12-hour format of the hour without leading zeros (1 to 12)
  • :: Literal colon separator
  • i: Minutes with leading zeros (00 to 59)
  • A: AM/PM indicator (uppercase)

Enter this format string in the parse_timestamp filter and check, update, and save the function.

Step 4: Set the Time Zone (Optional)

If you need to handle different time zones, you can set the time zone when parsing the timestamp.

  1. In the parse_timestamp filter, click on the "Time Zone" field.
  2. Search for your desired time zone in the list or use the TZ database name (e.g., Europe/Berlin).
  3. Check, update, and save the function.

Step 5: Format and Display the Timestamp

Finally, you can format and display the parsed timestamp in your desired format and time zone.

  1. Add a format_timestamp filter to your function.
  2. Set the "Time Zone" field to the desired time zone (e.g., America/Los_Angeles).
  3. Set the "Format" field to the desired output format (e.g., r for a human-readable format like Thu, 21 Dec 2017 16:08:00 +0200).
  4. Check, update, and save the function.
  5. Return the formatted timestamp in your response.

And that's it! You now have a Unix timestamp that you can store, send, or use as needed in your application.

Remember, this process can be adapted to different date and time formats by adjusting the format string in the parse_timestamp filter. Xano's documentation provides a comprehensive list of format characters to help you build the correct format string.

Happy coding (or no-coding)!

Sign up for Xano

Join 100,000+ people already building with Xano.

Start today and scale to millions.

Start building for free