Skip to content

timestampToDateTimeAmPm() Helper Method

The timestampToDateTimeAmPm() Helper Method module provides a helper function that takes in a UNIX Timestamp number argument and returns a formatted string date and time of that timestamp, in 12-hour format including the applicable am/pm value.

The method's second optional property accepts one of two string values of either "long" or "short", where the default short value outputs 3 character abbreviated month names, while the long value outputs full length month names.

Lastly, the method's third optional property currently accepts one of three string values of either "english", "french", or "spanish", where the default english value outputs full/short month names using English words, or full/short month names using French or Spanish words respectively.

Lastly, the method's fourth and fifth optional properties allow you to specify the AM and PM strings respectively. The default is uppercase "AM" and "PM" values, so you can explicitly switch them to lowercase as needed.

Import

To import the timestampToDateTimeAmPm() Helper Method:

javascript
import { timestampToDateTimeAmPm } from '@obewds/vueventus'

Arguments

Returns: String

ArgsTypeStatusDescription
timestamptimestampRequiredA UNIX Timestamp number value
formatStringOptionalExpects a string with a value of either "short" or "long" and sets either short or long form verbiage for the output traditional date string
languageStringOptionalExpects a string with a value of either "english", "french" or "spanish" and sets the langue of the output traditional date string's month value
amStringOptionalExpects a string with a value of either "am" or "AM" and sets the output am value
pmStringOptionalExpects a string with a value of either "pm" or "PM" and sets the output am value

Use Examples

javascript
console.log(
    timestampToDateTimeAmPm( 1660166705 )
)
// returns (string): 'Aug 10, 2022 5:25:05 PM'
    
console.log(
    timestampToDateTimeAmPm( 1660166705, 'long' )
)
// returns (string): 'August 10, 2022 5:25:05 PM'
    
console.log(
    timestampToDateTimeAmPm( 1660166705, 'long', 'french' )
)
// returns (string): 'Août 10, 2022 5:25:05 PM'
    
console.log(
    timestampToDateTimeAmPm( 1660166705, 'long', 'spanish', 'am', 'pm' )
)
// returns (string): 'Agosto 10, 2022 5:25:05 pm'

Module Code

ts
// ./src/helpers/timestampToDateTimeAmPm.ts

import monthNames from './monthNames'
import padNumber from './padNumber'

export default function (timestamp: number|string, format: 'long'|'short' = 'short', language: 'english'|'french'|'spanish' = 'english', am: 'am'|'AM' = 'AM', pm: 'pm'|'PM' = 'PM'): string {

    var a = new Date(Number(timestamp) * 1000)
    var monthNamesObj = monthNames()
    var months = monthNamesObj[language][format]
    var year = a.getFullYear()
    var month = months[a.getMonth()]
    var date = a.getDate()
    var hours = a.getHours()
    var minutes = a.getMinutes()
    var seconds = a.getSeconds()
    var amPm = hours >= 12 ? pm : am
    hours = hours % 12
    /* c8 ignore start */
    hours = hours === 0 ? 12 : hours
    /* c8 ignore stop */
    
    return month + ' ' + padNumber(date) + ', ' + year + ' ' + hours + ':' + padNumber(minutes) + ':' + padNumber(seconds) + ' ' + amPm

}

Released under the MIT License