---
title: "JS时间格式化（短日期）"
author: "hsys"
date: "2010-08-05T14:21:11Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2010-08-05T14:21:11Z"
id: 729788
url: "https://hsys.iteye.com/blog/729788"
---

JS时间格式化（短日期），如new Date() 转成2010-08-05


```js

function ChangeDateToString(DateIn){
    var Year = 0;
    var Month = 0;
    var Day = 0;	
    var CurrentDate = "";		
    Year  = DateIn.getFullYear();
    Month  = DateIn.getMonth()+1;
    Day  = DateIn.getDate();	
    CurrentDate = Year + "-";
    if (Month >= 10 ){
        CurrentDate = CurrentDate + Month + "-";
    } else{
        CurrentDate = CurrentDate + "0" + Month + "-";
    }
    if (Day >= 10 ){
        CurrentDate = CurrentDate + Day ;
    } else{
        CurrentDate = CurrentDate + "0" + Day ;
    }
    return CurrentDate;
}

```