php - get date as timestamp using javascript -


this question has answer here:

$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

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -