Skip to content

Mysql Date Time Helpers

This guide covers time and date conversion/formatting functions in the framework, including timezone support and localization.


Converts 12-hour time format (AM/PM) to 24-hour format.

convertTo24HourFormat(time);
ParameterTypeDescription
timestringTime string in 12-hour format (e.g., “02:30 PM”, “12:00 AM”)
  • string: Time in 24-hour format (e.g., “14:30”, “00:00”)
// Convert PM time
convertTo24HourFormat("02:30 PM"); // Returns: "14:30"
convertTo24HourFormat("11:45 PM"); // Returns: "23:45"
// Convert AM time
convertTo24HourFormat("09:15 AM"); // Returns: "09:15"
convertTo24HourFormat("12:00 AM"); // Returns: "00:00"
// 12 PM (noon)
convertTo24HourFormat("12:00 PM"); // Returns: "12:00"
  • Converting user input from 12-hour to 24-hour format
  • Preparing time data for database storage
  • Standardizing time formats

Converts time to MySQL DATETIME format with timezone support. Converts local time to UTC for database storage. Can also return only the time portion for MySQL TIME columns.

formatTimeForMySQL(time, (timezone = "+5:30"), (includeMilliseconds = false), (timeOnly = false));
ParameterTypeDefaultDescription
timeDate|number|stringrequiredTime input (Date object, timestamp, or time string)
timezonestring|number"+5:30"Timezone offset (e.g., “+5:30”, “IST”, or offset in minutes)
includeMillisecondsbooleanfalseWhether to include milliseconds in output. true = include, false = exclude
timeOnlybooleanfalseWhether to return only time portion. true = “HH:mm:ss”, false = full “YYYY-MM-DD HH:mm:ss”
  • string: MySQL DATETIME format string (e.g., “2024-01-15 14:30:45” or “2024-01-15 14:30:45.123”)
  • string: MySQL TIME format string (e.g., “14:30:45” or “14:30:45.123”) when timeOnly = true
  • null: If input is invalid
  • false (default): Returns format without milliseconds
    • Example: "2024-01-15 14:30:45"
  • true: Returns format with milliseconds
    • Example: "2024-01-15 14:30:45.123"

Purpose: Use true when you need microsecond precision for timestamps. Most use cases don’t need milliseconds, so false is the default.

  • false (default): Returns full DATETIME format
    • Example: "2024-01-15 14:30:45"
    • Use case: Storing complete date and time in DATETIME or TIMESTAMP columns
  • true: Returns only time portion
    • Example: "14:30:45"
    • Use case: Storing only time in MySQL TIME columns, or when you only need the time component

Purpose: Use true when storing time-only values (like “14:35:00”) in MySQL TIME columns, or when you only need the time portion without the date.

  • String formats: "+5:30", "+05:30", "+0530", "-5:00", "IST", "UTC", "GMT"
  • Number format: Offset in minutes (e.g., 330 for IST)
// Current time to MySQL format (IST timezone)
formatTimeForMySQL(new Date());
// Returns: "2024-01-15 08:30:45" (converted to UTC)
// With timestamp
formatTimeForMySQL(1705312245000);
// Returns: "2024-01-15 08:30:45"
// With time string (uses today's date)
formatTimeForMySQL("14:30:00");
// Returns: "2024-01-15 09:00:00" (14:30 IST = 09:00 UTC)
// IST (default)
formatTimeForMySQL(new Date(), "+5:30");
formatTimeForMySQL(new Date(), "IST");
// UTC
formatTimeForMySQL(new Date(), "UTC");
formatTimeForMySQL(new Date(), "+0:00");
// US Eastern Time (UTC-5)
formatTimeForMySQL(new Date(), "-5:00");
// Using offset in minutes
formatTimeForMySQL(new Date(), 330); // IST
formatTimeForMySQL(new Date(), -300); // UTC-5
formatTimeForMySQL(new Date(), "+5:30", true);
// Returns: "2024-01-15 08:30:45.123"
// Time only with milliseconds
formatTimeForMySQL(new Date(), "+5:30", true, true);
// Returns: "14:30:45.123"
// Return only time portion (for MySQL TIME columns)
formatTimeForMySQL(new Date(), "+5:30", false, true);
// Returns: "14:30:45"
// Time only with milliseconds
formatTimeForMySQL(new Date(), "+5:30", true, true);
// Returns: "14:30:45.123"
// With time string input
formatTimeForMySQL("14:35:00", "+5:30", false, true);
// Returns: "09:05:00" (converted to UTC)
// Common use case: Store time in TIME column
var timeValue = formatTimeForMySQL(new Date(), "+5:30", false, true);
// Returns: "14:30:45" - ready for MySQL TIME column
  • Storing timestamps in database: Convert local time to UTC for consistent storage
    • Use timeOnly = false for DATETIME/TIMESTAMP columns
    • Use timeOnly = true for TIME columns
  • API requests: Send properly formatted datetime to backend
  • Data logging: Standardize time format for logs
  • Time-only fields: Store time values without date (e.g., “14:35:00”)
  • Converts to UTC: This function converts the input time to UTC for database storage
  • Timezone handling: The timezone parameter specifies what timezone the input time is in
  • Default timezone: Defaults to IST (+5:30) if not specified
  • Time-only mode: When timeOnly = true, returns only “HH:mm:ss” format suitable for MySQL TIME columns
  • Parameter order: Parameters are positional, so you can skip optional ones but must provide them in order:
    • formatTimeForMySQL(time) - Uses all defaults
    • formatTimeForMySQL(time, timezone) - Custom timezone, default milliseconds and timeOnly
    • formatTimeForMySQL(time, timezone, includeMilliseconds) - Custom timezone and milliseconds
    • formatTimeForMySQL(time, timezone, includeMilliseconds, timeOnly) - All parameters

Converts MySQL UTC DATETIME to a specific timezone for display. Converts UTC from database to local timezone.

formatMySQLTimeFromUTC(mysqlDateTime, (timezone = "+5:30"), (outputFormat = null));
ParameterTypeDefaultDescription
mysqlDateTimestring|Date|numberrequiredMySQL DATETIME string (UTC) or Date object or timestamp
timezonestring|number"+5:30"Target timezone offset (e.g., “+5:30”, “IST”)
outputFormatstringnullFormat string (e.g., “YYYY-MM-DD HH:mm:ss”, “DD.MM.YYYY”)
  • Date object: If outputFormat is null
  • string: Formatted string in the target timezone if format is provided
  • null: If input is invalid
PlaceholderDescriptionExample
YYYY4-digit year2024
YY2-digit year24
MM2-digit month01, 12
DD2-digit day01, 31
HH24-hour format00-23
hh12-hour format01-12
mmMinutes00-59
ssSeconds00-59
sssMilliseconds000-999
AMPMAM/PMAM, PM
// MySQL UTC datetime string
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "+5:30");
// Returns: Date object for 2024-01-15 14:00:45 IST
// With format
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "+5:30", "DD.MM.YYYY");
// Returns: "15.01.2024"
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "+5:30", "DD.MM.YYYY HH:mm");
// Returns: "15.01.2024 14:00"
// Date only
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "+5:30", "DD.MM.YYYY");
// Returns: "15.01.2024"
// Date and time
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "+5:30", "DD.MM.YYYY HH:mm:ss");
// Returns: "15.01.2024 14:00:45"
// 12-hour format
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "+5:30", "DD.MM.YYYY hh:mm AMPM");
// Returns: "15.01.2024 02:00 PM"
// Time only
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "+5:30", "HH:mm");
// Returns: "14:00"
// Short date
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "+5:30", "DD.MM.YY");
// Returns: "15.01.24"
// IST
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "+5:30", "DD.MM.YYYY HH:mm");
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "IST", "DD.MM.YYYY HH:mm");
// UTC
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "UTC", "DD.MM.YYYY HH:mm");
// Returns: "15.01.2024 08:30"
// US Eastern Time
formatMySQLTimeFromUTC("2024-01-15 08:30:45", "-5:00", "DD.MM.YYYY HH:mm");
// Returns: "15.01.2024 03:30"
// Date object
formatMySQLTimeFromUTC(new Date(), "+5:30", "DD.MM.YYYY");
// Timestamp
formatMySQLTimeFromUTC(1705312245000, "+5:30", "DD.MM.YYYY HH:mm");
  • Displaying database timestamps: Convert UTC from database to user’s local timezone
  • Date formatting: Format dates for display in various formats
  • Localization: Show dates in user’s preferred timezone
  • Converts from UTC: This function converts UTC time from database to target timezone
  • Default timezone: Defaults to IST (+5:30) if not specified
  • Returns Date object: If no format is provided, returns a Date object

Formats dates with language support and number conversion. Supports multiple input formats and output formats with localization.

formatDate(inputDate, outputFormat);
ParameterTypeDescription
inputDateDate|number|stringDate input (Date object, timestamp, or various string formats)
outputFormatstringFormat string (e.g., “DD.MM.YYYY”, “DD MMMM YYYY”)
  • string: Formatted date string
  • null: If input is invalid
  • Date object: new Date()
  • Timestamp: 1705312245000
  • ISO format: "2024-01-15T14:30:45.123Z"
  • MySQL format: "2024-01-15" or "2024-01-15 14:30:45"
  • DD/MM/YYYY: "15/01/2024"
  • DD.MM.YYYY: "15.01.2024"
  • DD/MM/YY: "15/01/24" (assumes 20YY if < 50, else 19YY)
  • JavaScript toString: "Mon Feb 12 2024 18:45:30 GMT+0530"
PlaceholderDescriptionExample
YYYY4-digit year2024
YY2-digit year24
MM2-digit month01, 12
DD2-digit day01, 31
DoDay with ordinal1st, 2nd, 3rd, 15th
OOOShort month nameJan, Feb, Mar (localized)
NNNNFull month nameJanuary, February (localized)
HH24-hour format00-23
hh12-hour format01-12
mmMinutes00-59
ssSeconds00-59
AMPMAM/PMAM, PM

The function automatically uses the current language from global("current_language"):

  • English: Standard English month names and numbers
  • Hindi: Hindi month names and Devanagari numerals (०-९)
  • Kannada: Kannada month names and Kannada numerals (೦-೯)

Number conversion can be enabled/disabled via global("convert_number").

// Current date
formatDate(new Date(), "DD.MM.YYYY");
// Returns: "15.01.2024"
// With timestamp
formatDate(1705312245000, "DD.MM.YYYY");
// Returns: "15.01.2024"
// With string input
formatDate("2024-01-15", "DD.MM.YYYY");
// Returns: "15.01.2024"
formatDate("15/01/2024", "DD.MM.YYYY");
// Returns: "15.01.2024"
// Date only
formatDate(new Date(), "DD.MM.YYYY"); // "15.01.2024"
formatDate(new Date(), "DD/MM/YYYY"); // "15/01/2024"
formatDate(new Date(), "DD-MM-YYYY"); // "15-01-2024"
formatDate(new Date(), "DD.MM.YY"); // "15.01.24"
// With month names
formatDate(new Date(), "DD OOO YYYY"); // "15 Jan 2024"
formatDate(new Date(), "DD NNNN YYYY"); // "15 January 2024"
formatDate(new Date(), "DD Do NNNN YYYY"); // "15th January 2024"
// With time
formatDate(new Date(), "DD.MM.YYYY HH:mm"); // "15.01.2024 14:30"
formatDate(new Date(), "DD.MM.YYYY hh:mm AMPM"); // "15.01.2024 02:30 PM"
formatDate(new Date(), "DD.MM.YYYY HH:mm:ss"); // "15.01.2024 14:30:45"
// If current language is Hindi and convert_number is true
formatDate(new Date(), "DD.MM.YYYY");
// Returns: "१५.०१.२०२४" (with Devanagari numerals)
formatDate(new Date(), "DD NNNN YYYY");
// Returns: "१५ जनवरी २०२४" (Hindi month name)
// If current language is Kannada
formatDate(new Date(), "DD NNNN YYYY");
// Returns: "೧೫ ಜನವರಿ ೨೦೨೪" (Kannada month name and numerals)
// MySQL format
formatDate("2024-01-15", "DD.MM.YYYY");
// Returns: "15.01.2024"
// ISO format
formatDate("2024-01-15T14:30:45Z", "DD.MM.YYYY HH:mm");
// Returns: "15.01.2024 14:30"
// DD/MM/YYYY format
formatDate("15/01/2024", "DD.MM.YYYY");
// Returns: "15.01.2024"
// DD/MM/YY format (2-digit year)
formatDate("15/01/24", "DD.MM.YYYY");
// Returns: "15.01.2024" (assumes 2024)
formatDate("15/01/99", "DD.MM.YYYY");
// Returns: "15.01.1999" (assumes 1999 for years >= 50)
  • Display dates: Format dates for user display
  • Localization: Show dates in user’s language with localized month names and numerals
  • Date parsing: Parse various date formats into a standard format
  • UI display: Format dates for forms, lists, and reports
  • Language support: Automatically uses current language from global variables
  • Number conversion: Respects global("convert_number") setting
  • Flexible input: Accepts many common date formats
  • Ordinal suffixes: Supports “1st”, “2nd”, “3rd”, etc. with Do placeholder

Calculates the difference between a given date and today in specified units.

dateDiff(givenDate, (unit = "days"));
ParameterTypeDefaultDescription
givenDatestringrequiredDate string in DD.MM.YYYY format
unitstring"days"Unit of difference: “years”, “months”, “days”, “hours”, “minutes”, “seconds”
  • number: Difference in the specified unit (rounded down)
  • "years" - Difference in years
  • "months" - Difference in months (approximate, 30 days per month)
  • "days" - Difference in days
  • "hours" - Difference in hours
  • "minutes" - Difference in minutes
  • "seconds" - Difference in seconds
// Today is 2024-01-20
// Days difference
dateDiff("15.01.2024", "days"); // Returns: 5 (5 days ago)
dateDiff("25.01.2024", "days"); // Returns: -5 (5 days in future)
// Months difference
dateDiff("20.12.2023", "months"); // Returns: 1 (1 month ago)
dateDiff("20.02.2024", "months"); // Returns: -1 (1 month in future)
// Years difference
dateDiff("20.01.2023", "years"); // Returns: 1 (1 year ago)
dateDiff("20.01.2025", "years"); // Returns: -1 (1 year in future)
// Hours difference
dateDiff("20.01.2024", "hours"); // Returns: ~0 (same day)
dateDiff("19.01.2024", "hours"); // Returns: ~24 (1 day = 24 hours)
// Minutes difference
dateDiff("20.01.2024", "minutes"); // Returns: ~0 (same day)
  • Age calculation: Calculate age from birth date
  • Deadline tracking: Check how many days until/since a deadline
  • Date comparisons: Compare dates to today
  • Expiry checks: Check if something has expired
  • Input format: Only accepts DD.MM.YYYY format
  • Negative values: Returns negative values for future dates
  • Rounded down: Uses Math.floor() to round down the result
  • Month approximation: Months are calculated as 30 days (approximate)

Example 1: Storing and Displaying Timestamps

Section titled “Example 1: Storing and Displaying Timestamps”
// User selects time: 2:30 PM IST
var userTime = "02:30 PM";
// Convert to 24-hour format
var time24 = convertTo24HourFormat(userTime); // "14:30"
// Create date with this time
var date = new Date();
date.setHours(14, 30, 0);
// Store in database (convert to UTC) - Full DATETIME
var mysqlDateTime = formatTimeForMySQL(date, "+5:30", false, false);
// Returns: "2024-01-15 09:00:00" (14:30 IST = 09:00 UTC)
// Store only time in TIME column
var mysqlTime = formatTimeForMySQL(date, "+5:30", false, true);
// Returns: "09:00:00" (time only, converted to UTC)
// Later, retrieve and display
var displayTime = formatMySQLTimeFromUTC(mysqlDateTime, "+5:30", "DD.MM.YYYY hh:mm AMPM");
// Returns: "15.01.2024 02:30 PM"
// User enters time: 14:35:00
var userTime = "14:35:00";
// Store as TIME value (timeOnly = true)
var mysqlTime = formatTimeForMySQL(userTime, "+5:30", false, true);
// Returns: "09:05:00" (14:35 IST = 09:05 UTC)
// Use in SQL INSERT for TIME column
// INSERT INTO schedule (start_time) VALUES ('09:05:00');

Example 2: Date Formatting with Localization

Section titled “Example 2: Date Formatting with Localization”
// Get date from API (MySQL format)
var apiDate = "2024-01-15 14:30:45";
// Format for display (IST timezone)
var formatted = formatMySQLTimeFromUTC(apiDate, "+5:30", "DD.MM.YYYY");
// Returns: "15.01.2024"
// Format with month name
var withMonth = formatDate(formatted, "DD NNNN YYYY");
// Returns: "15 January 2024" (or localized if language is set)
// If language is Hindi and convert_number is true
// Returns: "१५ जनवरी २०२४"
// Check if order is overdue
var returnDate = "25.01.2024";
var daysLeft = dateDiff(returnDate, "days");
if (daysLeft < 0) {
console.log("Order is overdue by " + Math.abs(daysLeft) + " days");
} else if (daysLeft === 0) {
console.log("Order is due today");
} else {
console.log("Order is due in " + daysLeft + " days");
}
// 1. User enters date in form: "15.01.2024"
var userDate = "15.01.2024";
// 2. Format for display
var displayDate = formatDate(userDate, "DD NNNN YYYY");
// Returns: "15 January 2024"
// 3. Convert to MySQL format for storage
var dateObj = new Date(userDate.split(".").reverse().join("-"));
var mysqlDate = formatTimeForMySQL(dateObj, "+5:30");
// Returns: "2024-01-15 00:00:00"
// 4. Store in database...
// 5. Retrieve and display
var retrievedDate = formatMySQLTimeFromUTC(mysqlDate, "+5:30", "DD.MM.YYYY");
// Returns: "15.01.2024"
// 6. Calculate days since
var daysSince = dateDiff(retrievedDate, "days");
// Returns: difference in days from today

FunctionPurposeInputOutputTimezone
convertTo24HourFormat()Convert 12h to 24h”02:30 PM""14:30”N/A
formatTimeForMySQL()Local → UTC for DBDate/TimeMySQL DATETIME/TIMEConverts to UTC
formatMySQLTimeFromUTC()UTC → Local for displayMySQL DATETIMEFormatted stringConverts from UTC
formatDate()Format any dateDate/TimeFormatted stringUses local time
dateDiff()Calculate differenceDD.MM.YYYYNumberN/A
// Store current time in database (full DATETIME)
var mysqlDateTime = formatTimeForMySQL(new Date(), "+5:30", false, false);
// Returns: "2024-01-15 14:30:45"
// Store only time in TIME column
var mysqlTime = formatTimeForMySQL(new Date(), "+5:30", false, true);
// Returns: "14:30:45"
// Store with milliseconds (high precision)
var mysqlDateTimeMs = formatTimeForMySQL(new Date(), "+5:30", true, false);
// Returns: "2024-01-15 14:30:45.123"
// Display database time to user
var displayTime = formatMySQLTimeFromUTC(mysqlDateTime, "+5:30", "DD.MM.YYYY hh:mm AMPM");
// Format any date
var formatted = formatDate(new Date(), "DD.MM.YYYY");
// Check days until date
var days = dateDiff("25.01.2024", "days");

  1. Timezone Default: All functions default to IST (+5:30) if timezone is not specified
  2. UTC Storage: Always store times in UTC in the database using formatTimeForMySQL()
  3. Local Display: Always convert from UTC to local timezone for display using formatMySQLTimeFromUTC()
  4. Language Support: formatDate() automatically uses current language settings
  5. Input Flexibility: Most functions accept Date objects, timestamps, and various string formats
  6. Error Handling: Functions return null if input is invalid
  7. Boolean Parameters:
    • includeMilliseconds: false = no milliseconds (default), true = include milliseconds
    • timeOnly: false = full DATETIME format (default), true = time only “HH:mm:ss” format
  8. MySQL Column Types:
    • Use timeOnly = false for DATETIME or TIMESTAMP columns
    • Use timeOnly = true for TIME columns