php - get date as timestamp using javascript -
this question has answer here:
- how format javascript date 36 answers
$a = date('y-m-d h:i:s'); echo $a; result:
2017-08-18 09:14:02 is there way date in same format using javascript / jquery ?
here's approach easy understand newer javascript developers come oop background.
var date = new date(); alert(date.getfullyear() + "-" + ("0" + (date.getmonth() + 1)).slice(-2) + "-" + ("0" + date.getdate()).slice(-2) + " " + ("0" + date.gethours()).slice(-2) + ":" + ("0" + date.getminutes()).slice(-2) + ":" + ("0" + date.getseconds()).slice(-2)); date.getmonth() + 1 because months 0 indexed.
edit: above solution adds leading zeros getmonth() , getday(). slice(-2) call common way getting last 2 characters string.
for example, if date.getmonth() returns 9. 09, , slice(-2) return me same 09.
but if date.getmonth() returns 10. 010, , slice(-2) return last 2 characters again. so, 10.
the other answers correct, 1 easier understand beginners perspective.
Comments
Post a Comment