Skip to content

formatDateMMDDYYYY() Helper Method

The formatDateMMDDYYYY() Helper Method module provides a helper function that converts a passed JavaScript Date object and an optional string separator, and returns a numerical 2 digit month, 2 digit day, and 4 digit year string with each number separated by a / character by default.

Import

To import the formatDateMMDDYYYY() Helper Method:

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

Arguments

Returns: String

ArgsTypeStatusDescription
dateDate objectRequiredA JavaScript Date object set to the desired date
separatorStringOptionalA character or string to use as a separator for the output date values

Use Examples

javascript
console.log( formatDateMMDDYYYY( new Date('January 10, 2020') ) )
// returns (string): '01/10/2020'

console.log( formatDateMMDDYYYY( new Date('December 9, 2020'), '-' ) )
// returns (string): '12-09-2020'

Module Code

ts
// ./src/helpers/formatDateMMDDYYYY.ts

import padNumber from './padNumber'

export default function (date: Date, separator = '/'): string {
    return [
        padNumber(date.getMonth() + 1),
        padNumber(date.getDate()),
        date.getFullYear(),
    ].join(separator)
}

Released under the MIT License