DEV Community

Richard Haines
Richard Haines

Posted on

1 1

Date object snippet

Getting the locale date

The toLocaleDateString method on the date object lets you pass in the locale of choice and returns a string formatted date with forward slash separators.

let today = new Date().toLocaleDateString('en-gb');

// today => 24/09/2020
Enter fullscreen mode Exit fullscreen mode

Replacing the slash

To replace the forward slash with a dash is as easy as manipulating the string with a replace regex.

let today = new Date().toLocaleDateString('en-gb').replace(/\//g, '-');

// today => 24-09-2020
Enter fullscreen mode Exit fullscreen mode

Written representation

To get the written representation of the date in the chosen locale toLocaleDateString accepts a second options param where you can specify

  • weekday
  • year
  • month
  • day
const options = {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
};
let today = new Date().toLocaleDateString('en-gb', options);

// today => Thursday, 24 September 2020
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay