---
title: "JS得到当前时间字符串"
author: "ljf_home"
date: "2014-12-30T13:29:45Z"
tags: ["JS", "当前时间"]
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2014-12-30T13:30:31Z"
id: 2171166
url: "https://rsy.iteye.com/blog/2171166"
---

```
//得到当前时间字符串，格式为：YYYY-MM-DD HH:MM:SS  
function CurentTime()
{ 
	var now = new Date();
       
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日
       
    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分
	var ss=now.getSeconds();			//秒
       
    var clock = year + "-";
       
    if(month < 10) clock += "0";       
    clock += month + "-";
       
    if(day < 10) clock += "0"; 
    clock += day + " ";
       
    if(hh < 10) clock += "0";
    clock += hh + ":";

    if (mm < 10) clock += '0'; 
    clock += mm+ ":";
		
    if (ss < 10) clock += '0'; 
	clock += ss;

    return(clock); 
}
```