如何在javascript中将YYYY-MM-DDT00:00:00转换为YYYY / MM / DD?

时间:2017-08-07 09:59:51

标签: javascript date datetime

我将某个日期(以毫秒为单位)转换为日期格式,如下所示:

var dateFrom = new Date(1312883657720); //dateFrom = 2011-08-09T12:57:01

现在我需要以这种格式使用dateFrom(2011-08-09T12:57:01)的值:2011/08/09。我似乎找不到删除时间字符串的日期方法,是否需要解决方法?

4 个答案:

答案 0 :(得分:1)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(sdf.format(new Date()));

或其他方法使用日历,

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
dateWithoutTime = cal.getTime();

答案 1 :(得分:0)

您需要使用对象

形成一个字符串
var dateFrom = new Date(1312883657720); //dateFrom = 2011-08-09T12:57:01
var humanDate = dateFrom.getFullYear()
                + "/" + ("0" + (dateFrom.getMonth()+1)).slice(-2)
                + "/" + ("0" + dateFrom.getDate()).slice(-2);

// ("0" + dateFrom.method()).slice(-2) is used to add the leading zero

console.log(humanDate); // 2011/08/09

答案 2 :(得分:0)

使用此

<div id="res">res</div>

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('/');
}
document.getElementById('res').innerHTML =  formatDate('2011-08-09T12:57:01') ;

产出:2011/08/09

答案 3 :(得分:0)

尝试以下两种方法中的任何一种

var newDate = moment(new Date(1312883657720)).format(YYYY/MM/DD);

var newDate = new Date(1312883657720);
newDate = newDate.getFullYear()+'/'+newDate.getMonth()+'/'+newDate getDate()