JavaScript Date Formatting with strftime

Today, we are going to explore JavaScript Date Formatting with strftime.

Knowing how to format dates properly is dominant for making applications that are dynamic and easy for users to understand.

You’ll also learn the power of strftime for formatting dates and times in JavaScript in an easy way.

What is strftime in JavaScript?

The strftime is a method used to format dates and times as strings. It is commonly used in languages such as Ruby and Python, but it is not a native function in JavaScript.

However, there are several libraries available that provide similar functionality, such as moment.js and strftime.js.

The strftime() method is used to get a local time/date format according to the locale settings.

How to set a strftime in JavaScript?

There is no built-in strftime function in JavaScript, but you can use various libraries that provide similar functionality.

Solution 1: Use libraries

One of the most used libraries is Moment.js, wherein you are allowed to format dates and times using the strftime syntax.

Here is the following syntax, using Moment.js:

 moment().format('dd, d of MMMM')

Here is the following syntax, using strftime for JavaScript:

 strftime('%B %d, %Y %H:%M:%S')

For instance, you can utilize the format method to format a date object like this:

var now = moment();
var formattedDate = now.format('MMMM Do YYYY, h:mm:ss a');

console.log(formattedDate);

Note: you have included the Moment.js library, before you can use the above code like.

Solution 2:Use native JavaScript methods

You can also use libraries that bring the strftime function to JavaScript.

For example, strftime.js provides many specifiers from C-language and Ruby.

Another option is thdoan/strftime, which is a lightweight and straightforward library.

let date = new Date(Date.UTC(2023, 5, 30, 12, 0, 0)),
options = {weekday: 'short', month: 'short', day: 'numeric'};
console.log(date.toLocaleString('en-US', options));

Output:

Fri, Jun 30

As you can see, the result will be “Fri, Jun 30”, which corresponds to “Friday, June 30th.”

Please take note that the month value provided to the Date.UTC method is 5 instead of 6, as JavaScript considers months to be zero-indexed (where January is 0, February is 1, and so on).

What are the common date formatting?

JavaScript strftime allows you to customize date formats using various tokens. Let’s take a look at some of the commonly used ones.

Year%Y

The %Y token represents the four-digit year. For example, %Y will display “2023.”

Month – %m

The %m token represents the two-digit month. For example, %m will display “30” for June.

Day – %d

The %d token represents the two-digit day of the month. For example, %d will display “30.”

Hour – %H

The %H token represents the two-digit hour in 24-hour format. For example, %H will display “16” for 4 PM.

Second – %S

The %S token represents the two-digit second. For example, %S will display “50.”

AM/PM

The %A token represents the AM/PM indicator. For example, %A will display “AM” or “PM” based on the time.

Advanced Date Formatting

JavaScript strftime gives you powerful tools to format dates exactly how you want. You can combine different tokens to create complex date formats. Let’s learn some advanced techniques for formatting dates.

Ordinal Day

The %o token displays the day of the month with an ordinal suffix, like “1st”, “2nd”, “3rd”, etc.

Weekday

The %A token shows the complete weekday name like “Sunday” or “Monday.”

Timezone

The %Z token lets you add the timezone abbreviation to the formatted date.

Unix Timestamp

The %s token gives you the Unix timestamp, which counts the number of seconds since January 1, 1970.

Milliseconds

The %L token represents milliseconds in the formatted date.

Conclusion

In conclusion, this article discusses the JavaScript strftime. Although JavaScript does not have a built-in strftime function, there are libraries available such as Moment.js, strftime.js, and thdoan/strftime, that offer similar functionality.

These libraries allow you to format dates and times according to various tokens, providing flexibility in displaying the year, month, day, hour, second, AM/PM indicator, and more.

By utilizing these libraries, you can easily customize and format dates in JavaScript for improved user experience and application functionality.

We are hoping that this article provides you with enough information that helps you understand the JavaScript strftime.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment