I\'m a Java developer and I\'m used to the SimpleDateFormat class that allows me to format any date to any format by settings a timezone.
Date date = new Dat
Since my requirement was a typescript solution but I stumbled here I used this answer to write my typescript function.
Based on answer above, a function in typescript which converts a timestamp or a date object into a formatted local time string.
const formatDateString = (date_or_ts:Date|number):string=>{
let obj:Date;
if(typeof date_or_ts === "number"){
obj = new Date(date_or_ts*1000);
// obj=new Date(obj.getTime()+obj.getTimezoneOffset()*60000+timezone*3600000);
}else{
obj = date_or_ts;
}
const format = "dd-MM-yyyy hh:mm:ss";
let two=function(s:number){
return s<10?"0"+s:s+"";
}
return format.replace(/dd|MM|yyyy|hh|mm|ss/g, function(pattern){
const months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
switch(pattern){
case "dd" : return two(obj.getDate()).toString();
case "MM" : return months[obj.getMonth()];
case "yyyy" : return obj.getFullYear().toString();
case "hh" : return two(obj.getHours()).toString();
case "mm" : return two(obj.getMinutes()).toString();
case "ss" : return two(obj.getSeconds()).toString();
default: return "";
}
});
}