last day (14 days later) » 

8:47 AM
0
A: Format datetime in Typescript

Amit Suhagyou can use moment.js. install moment js in your project moment("2016-01-17T:08:44:29+0100").format('MM/DD/YYYY'); for more format option check Moment.format()

 
I cannot use moment.js
 
for html part you can do using pipe <p>Or if you prefer, {{today | date:'fullDate'}}</p>
 
I'm using some third party module for UI and I cannot format in HTML. So i have to handle the format in typescript and send it for rendering
 
if you want to do it just using ts then first extract all day,month,year then concat them in string . i use mostly moment.js. may i know why can't you use moment.js ? @Protagonist
 
Correct. I know how to concat. But how to extract the date and time into strings?
 
8:47 AM
typescript is superset of javascript. so every date method will work in typescript. just extract getDay() month() in a variable and in new variable just add all of them like var stringdate=day+'/'+month+'/'+year. in stringdate var you will get string format of you date
hello :)
sir why can't you use moment.js ?
 
Hi
I dont have permission to use it
Working in a client project
 
ok
well in that case var newdate = new Date(yourdatehere);
then try to extract all month day year and time
and concate them
thats it
 
Just give me a moment... will check
 
ok
function GetTime(date) {
var day = zeroPad(date.getDate(), 2);
var month = zeroPad(date.getMonth() + 1, 2);
var year = zeroPad(date.getFullYear(), 4);
var hour = zeroPad(date.getHours(), 2);
var minute = zeroPad(date.getMinutes(), 2);
var second = zeroPad(date.getSeconds(), 2);
return day + "." + month + "." +
year + " " + hour + ":" +
minute + ":" + second;
}
you can use date function from this
 
9:14 AM
hi
 
hi
did it work ?
hello are you there ?
 
9:31 AM
??
 
 
2 hours later…
11:28 AM
Hi sorry I was slept off
what is zeropad? isn't it possible to use simple date function to achieve this?
ok. zeropad used for padding. but I cannot so npm install zeropad
 

last day (14 days later) »