Is There a Built-In Function or Plugin to Handle Date Formatting in JavaScript

Is there a built-in function or plugin to handle date formatting in JavaScript?

No, there is nothing built-in for Dateobjects, but there are a bunch of libraries to deal with and format them:

  • date.js
  • moment.js
  • XDate
  • Date and Date.Extras in Mootools
  • Date in Sugar
  • Dojo.date
  • a few functions in Mochikit
  • DateFormat (only PHP's date)
  • date at php.js
  • DataType in YUI, especially for i18n
  • date-functions.js, used especially by JQuery datetimepicker plugin

Built-in function in Javascript to format date

use a library or a function... don't see any other way.

function getDateString(d){
return
["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][d.getDay()]+" "+
["January","February","March","April","May","June","July","August","September","October","November","December"][d.getMonth()]+" "+
d.getDate()+", "+
d.getFullYear();
}

get Javascript date formatted as 2011-04-01

   function getFormattedDate() {
var date = new Date();
var str = date.getFullYear() + "-" + getFormattedPartTime(date.getMonth()) + "-" + getFormattedPartTime(date.getDate()) + " " + getFormattedPartTime(date.getHours()) + ":" + getFormattedPartTime(date.getMinutes()) + ":" + getFormattedPartTime(date.getSeconds());

return str;
}

function getFormattedPartTime(partTime){
if (partTime<10)
return "0"+partTime;
return partTime;
}

Formatting and pretty printing dates with jquery

http://timeago.yarp.com/

Get current date in DD-Mon-YYY format in JavaScript/Jquery

There is no native format in javascript for DD-Mon-YYYY.

You will have to put it all together manually.

The answer is inspired from :
How do I format a date in JavaScript?

// Attaching a new function  toShortFormat()  to any instance of Date() class

Date.prototype.toShortFormat = function() {

const monthNames = ["Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"];

const day = this.getDate();

const monthIndex = this.getMonth();
const monthName = monthNames[monthIndex];

const year = this.getFullYear();

return `${day}-${monthName}-${year}`;
}

// Now any Date object can be declared
let anyDate = new Date(1528578000000);

// and it can represent itself in the custom format defined above.
console.log(anyDate.toShortFormat()); // 10-Jun-2018

let today = new Date();
console.log(today.toShortFormat()); // today's date

Format a date string in javascript

Use Moment.js and the .format function.

moment('2017-06-10T16:08:00').format('MM/DD/YYYY');

Will output

06/10/2017

Beside the format function Moment.js will enrich you will alot more useful functions.

What is the right JSON date format?

JSON itself does not specify how dates should be represented, but JavaScript does.

You should use the format emitted by Date's toJSON method:

2012-04-23T18:25:43.511Z

Here's why:

  1. It's human readable but also succinct

  2. It sorts correctly

  3. It includes fractional seconds, which can help re-establish chronology

  4. It conforms to ISO 8601

  5. ISO 8601 has been well-established internationally for more than a decade

  6. ISO 8601 is endorsed by W3C, RFC3339, and XKCD

That being said, every date library ever written can understand "milliseconds since 1970". So for easy portability, ThiefMaster is right.



Related Topics



Leave a reply



Submit