LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Pure JavaScript Date Functions Lightweight Vanilla JavaScript

Pure JavaScript Date Functions Lightweight Vanilla JavaScript

Pure-JavaScript-Date-Functions-Lightweight-Vanilla-JavaScript
Javascript Date object is not very well-loved in the community. It is something Javascript adopted from Java but both grew in different directions. Its API can be confusing and lacks a lot of methods and information that you often need when working with date and time in your projects.

I’ve used a variety of tools over the years to manage date and time functionality in JavaScript. Here I am documenting the Vanilla JavaScript patterns for my own use.

These functions are very useful when dealing with any type of date field.

They use the same format strings as the java.text.SimpleDateFormat class, with a few minor exceptions.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Functions Include:

  • isDate() – Check if a date is valid
  • compareDates() – See if one date is greater than another
  • formatDate() – Output a Date() object in any format
  • getDateFromFormat() – Parse a date string and return time in ms representing the date object
Recommended For You:
How To Detect If Caps Lock Is On Using Pure JavaScript?

isDate()

Check if a date is valid in your given format. (Note the difference between M (month) and m (minute)!!!)

Date String        Date Format    Result
January 1, 1900    MMM d, y       True

compareDates()

See if one date is greater than another.

Date String 1        Date String 2         Result
January 1, 1900      January 1, 1900       0

formatDate()

Output a Date() object in any format.

Current Date       Date Format    Output 
January 1, 1900    yyyy-MM-dd     1990-01-01

getDateFromFormat()

Parse a date string and return time in ms representing the date object

Current Date      Old Date Format     Convert To Format      Output 
January 1, 1900   MMM d, y            yyyy-MM-dd      1990-01-01

DateTime Format Information:

The format string consists of the following abbreviations:
Field        | Full Form          | Short Form
-------------|--------------------|-----------------------
Year         | yyyy (4 digits)    | yy (2 digits), y (2 or 4 digits)
Month        | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
             | NNN (abbr.)        |
Day of Month | dd (2 digits)      | d (1 or 2 digits)
Day of Week  | EE (name)          | E (abbr)
Hour (1-12)  | hh (2 digits)      | h (1 or 2 digits)
Hour (0-23)  | HH (2 digits)      | H (1 or 2 digits)
Hour (0-11)  | KK (2 digits)      | K (1 or 2 digits)
Hour (1-24)  | kk (2 digits)      | k (1 or 2 digits)
Minute       | mm (2 digits)      | m (1 or 2 digits)
Second       | ss (2 digits)      | s (1 or 2 digits)
AM/PM        | a                  |

NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm!

Examples:
 "MMM d, y" matches: January 01, 2000
                     Dec 1, 1900
                     Nov 20, 00
 "M/d/yy"   matches: 01/20/00
                     9/2/00
 "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM"

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
Awesome Windows-8 Style Loading On Desired Links In Your Webpage

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *