Skip to content

Instantly share code, notes, and snippets.

@codejets
Created January 4, 2018 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codejets/1d486737ea812bd1d1f4c07ff31c6346 to your computer and use it in GitHub Desktop.
Save codejets/1d486737ea812bd1d1f4c07ff31c6346 to your computer and use it in GitHub Desktop.
date format js
export const formatToStandardDate = (unixTimeStamp, format) => {
// *1000 to convert unix timestamp in ms to s
const currentDate = new Date(unixTimeStamp*1000);
const mon=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
const fullMon=['January','February','March','April','May','June','July','August','September','October','November','December'];
const days=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
const fullDays=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
let regex=new RegExp("DDDD","g");
format=format.replace(regex, fullDays[this.getDay()]);
regex=new RegExp("DDD","g");
format=format.replace(regex, days[this.getDay()]);
regex=new RegExp("dd","g");
format=format.replace(regex,this.getDate());
regex=new RegExp("MMMM","g");
format=format.replace(regex, fullMon[this.getMonth()]);
regex=new RegExp("MMM","g");
format=format.replace(regex, mon[this.getMonth()]);
regex=new RegExp("MM","g");
format=format.replace(regex,this.getMonth()+1);
regex=new RegExp("yyyy","g");
format=format.replace(regex, this.getFullYear());
regex=new RegExp("yy","g");
format=format.replace(regex, this.getFullYear().toString().substr(2,2));
regex=new RegExp("mm","g");
format=format.replace(regex, this.getMinutes());
regex=new RegExp("hh","g");
format=format.replace(regex, this.getHours());
regex=new RegExp("ss","g");
format=format.replace(regex, this.getSeconds());
regex=new RegExp("ms","g");
format=format.replace(regex, this.getMilliseconds());
return format;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment