Format Date trong JavaScript
For a long time, I've used libraries like Show I discovered that most developers do this a lot. And I thought that this was the best way until I recently figured out that we don’t always need to use libraries to format dates in JavaScript. In case you are curious to try this out, here is the code:👇
After trying this in your own code and seeing that it works, let's understand why it works and learn some other ways of formatting dates in JavaScript with just one line of code. How to Format Dates in JSGetting the date in JavaScript isn't usually a problem, but formatting these dates to suit your project can be cumbersome for beginners. Because of this, most people eventually end up using libraries. The most used method to get the date in JavaScript is the By default, when you run But having something like this in your web page or application is not professional and isn't easy to read. So this forces you to look for better ways to format these dates. Let’s take a look at some methods that operate on a date object. Date Methods in JavaScriptThere are so many methods that you can apply to the date object. You can use these methods to get information from a date object. Here are some of them:
And lots more… Unfortunately, most of these methods still needs a lot of code to convert the dates to the format we desire. For example, 5 will output 6 which stands for July. For me to use July in my project, I will need to have long code like this which can really be cumbersome:
Let’s take a look at two methods that you can use to format your dates in the best way so you can use them for your projects. The toDateString() Method in JavaScriptThe JavaScript 6 method returns the date portion of a date object in the form of a string using the following format:
One major downside to this method is our inability to manipulate the date output the way we want it. For example, it doesn’t give us the ability to show dates according to our language. Let’s take a look at another method which to me is still one of the best. The toLocaleDateString() Method in JavaScriptThis method returns the date object as a string using local conventions. It also takes in options as arguments which lets you/your applications customize the behavior of the function. Syntax:
Let's take a look a some examples and their outputs:
And you can also decide to only use locales. This will output the same information as the previous based on my browser's time zone.
You can also decide to twist the options as you wish. There are 4 basic options which are:
ConclusionThe date object has about seven formatting methods. Each of these methods gives you a specific value:
If you are looking for more advanced date formats, then you will need to create a custom format yourself. Check out the resources below to help you understand how to create custom date formats. Useful Resources
ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT Frontend Developer & Technical Writer If you read this far, tweet to the author to show them you care. Tweet a thanks Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started |