使用javascript格式化日期

使用javascript格式化日期,javascript,jquery,Javascript,Jquery,我现在有一个JS的小sinppet,它返回一个日期: function dateFormatter(date) { return date; } 返回: 2014年8月6日星期三14:43:58 GMT+0100(GMT标准时间) 如何调整该功能,使其显示类似:8月14日 我看到产出已经在8月完成了,但我想2014年至14日的日期需要做一些调整 提前感谢您应该尝试: 对于纯JS: function dateFormatter(date) { return date.toString()

我现在有一个JS的小sinppet,它返回一个日期:

function dateFormatter(date) {
  return date;
}
返回:

2014年8月6日星期三14:43:58 GMT+0100(GMT标准时间)

如何调整该功能,使其显示类似:8月14日

我看到产出已经在8月完成了,但我想2014年至14日的日期需要做一些调整

提前感谢

您应该尝试:

对于纯JS:

function dateFormatter(date) {
  return date.toString().split(' ')[1] + ' ' + date.getFullYear() % 100;
}
您应该尝试以下方法:

对于纯JS:

function dateFormatter(date) {
  return date.toString().split(' ')[1] + ' ' + date.getFullYear() % 100;
}
您应该尝试以下方法:

对于纯JS:

function dateFormatter(date) {
  return date.toString().split(' ')[1] + ' ' + date.getFullYear() % 100;
}
您应该尝试以下方法:

对于纯JS:

function dateFormatter(date) {
  return date.toString().split(' ')[1] + ' ' + date.getFullYear() % 100;
}

如果要从日期对象格式化,请使用以下命令:

function formatDate(date) { //takes a Date object
    var monthArr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']; //creates array of month names
    var month = monthArr[date.getMonth()]; //selects array element equal to getMonth(), which ranges from 0-11
    var year = date.getFullYear()%100; //takes the full year (4 digits) mod 100, so the last two digits
    var dateString = month + ' ' + year;
    return dateString;
}
function formatDate2(str) { //takes a string
    var dateArr = str.split(' '); //creates array from string, splitting by spaces
    var dateString = dateArr[1] + ' ' + dateArr[3].slice(2,4); //gets element 1 (month) and the last two digits of of element 3 (year)
    return dateString;
}
如果要格式化字符串(如您提供的字符串),请使用以下命令:

function formatDate(date) { //takes a Date object
    var monthArr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']; //creates array of month names
    var month = monthArr[date.getMonth()]; //selects array element equal to getMonth(), which ranges from 0-11
    var year = date.getFullYear()%100; //takes the full year (4 digits) mod 100, so the last two digits
    var dateString = month + ' ' + year;
    return dateString;
}
function formatDate2(str) { //takes a string
    var dateArr = str.split(' '); //creates array from string, splitting by spaces
    var dateString = dateArr[1] + ' ' + dateArr[3].slice(2,4); //gets element 1 (month) and the last two digits of of element 3 (year)
    return dateString;
}

如果要从日期对象格式化,请使用以下命令:

function formatDate(date) { //takes a Date object
    var monthArr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']; //creates array of month names
    var month = monthArr[date.getMonth()]; //selects array element equal to getMonth(), which ranges from 0-11
    var year = date.getFullYear()%100; //takes the full year (4 digits) mod 100, so the last two digits
    var dateString = month + ' ' + year;
    return dateString;
}
function formatDate2(str) { //takes a string
    var dateArr = str.split(' '); //creates array from string, splitting by spaces
    var dateString = dateArr[1] + ' ' + dateArr[3].slice(2,4); //gets element 1 (month) and the last two digits of of element 3 (year)
    return dateString;
}
如果要格式化字符串(如您提供的字符串),请使用以下命令:

function formatDate(date) { //takes a Date object
    var monthArr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']; //creates array of month names
    var month = monthArr[date.getMonth()]; //selects array element equal to getMonth(), which ranges from 0-11
    var year = date.getFullYear()%100; //takes the full year (4 digits) mod 100, so the last two digits
    var dateString = month + ' ' + year;
    return dateString;
}
function formatDate2(str) { //takes a string
    var dateArr = str.split(' '); //creates array from string, splitting by spaces
    var dateString = dateArr[1] + ' ' + dateArr[3].slice(2,4); //gets element 1 (month) and the last two digits of of element 3 (year)
    return dateString;
}

如果要从日期对象格式化,请使用以下命令:

function formatDate(date) { //takes a Date object
    var monthArr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']; //creates array of month names
    var month = monthArr[date.getMonth()]; //selects array element equal to getMonth(), which ranges from 0-11
    var year = date.getFullYear()%100; //takes the full year (4 digits) mod 100, so the last two digits
    var dateString = month + ' ' + year;
    return dateString;
}
function formatDate2(str) { //takes a string
    var dateArr = str.split(' '); //creates array from string, splitting by spaces
    var dateString = dateArr[1] + ' ' + dateArr[3].slice(2,4); //gets element 1 (month) and the last two digits of of element 3 (year)
    return dateString;
}
如果要格式化字符串(如您提供的字符串),请使用以下命令:

function formatDate(date) { //takes a Date object
    var monthArr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']; //creates array of month names
    var month = monthArr[date.getMonth()]; //selects array element equal to getMonth(), which ranges from 0-11
    var year = date.getFullYear()%100; //takes the full year (4 digits) mod 100, so the last two digits
    var dateString = month + ' ' + year;
    return dateString;
}
function formatDate2(str) { //takes a string
    var dateArr = str.split(' '); //creates array from string, splitting by spaces
    var dateString = dateArr[1] + ' ' + dateArr[3].slice(2,4); //gets element 1 (month) and the last two digits of of element 3 (year)
    return dateString;
}

如果要从日期对象格式化,请使用以下命令:

function formatDate(date) { //takes a Date object
    var monthArr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']; //creates array of month names
    var month = monthArr[date.getMonth()]; //selects array element equal to getMonth(), which ranges from 0-11
    var year = date.getFullYear()%100; //takes the full year (4 digits) mod 100, so the last two digits
    var dateString = month + ' ' + year;
    return dateString;
}
function formatDate2(str) { //takes a string
    var dateArr = str.split(' '); //creates array from string, splitting by spaces
    var dateString = dateArr[1] + ' ' + dateArr[3].slice(2,4); //gets element 1 (month) and the last two digits of of element 3 (year)
    return dateString;
}
如果要格式化字符串(如您提供的字符串),请使用以下命令:

function formatDate(date) { //takes a Date object
    var monthArr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']; //creates array of month names
    var month = monthArr[date.getMonth()]; //selects array element equal to getMonth(), which ranges from 0-11
    var year = date.getFullYear()%100; //takes the full year (4 digits) mod 100, so the last two digits
    var dateString = month + ' ' + year;
    return dateString;
}
function formatDate2(str) { //takes a string
    var dateArr = str.split(' '); //creates array from string, splitting by spaces
    var dateString = dateArr[1] + ' ' + dateArr[3].slice(2,4); //gets element 1 (month) and the last two digits of of element 3 (year)
    return dateString;
}

JS中的日期格式化是一个巨大的难题。您可以使用内置函数分解日期的各个部分,这些函数可以正常工作,但实现起来很慢,而且不会国际化。或者你也可以用它来照顾你,如果你想要轻量的话,写下你自己的,用它来支撑月份和年份。这似乎是一件非常简单的事情。JS中的日期格式化是一件非常痛苦的事情。您可以使用内置函数分解日期的各个部分,这些函数可以正常工作,但实现起来很慢,而且不会国际化。或者你也可以用它来照顾你,如果你想要轻量的话,写下你自己的,用它来支撑月份和年份。这似乎是一件非常简单的事情。JS中的日期格式化是一件非常痛苦的事情。您可以使用内置函数分解日期的各个部分,这些函数可以正常工作,但实现起来很慢,而且不会国际化。或者你也可以用它来照顾你,如果你想要轻量的话,写下你自己的,用它来支撑月份和年份。这似乎是一件非常简单的事情。JS中的日期格式化是一件非常痛苦的事情。您可以使用内置函数分解日期的各个部分,这些函数可以正常工作,但实现起来很慢,而且不会国际化。或者你也可以用它来照顾你,如果你想要轻量的话,写下你自己的,用它来支撑月份和年份。这似乎是一件非常简单的事情。它只是想在不加载插件等的情况下尽可能地保持这一重量。它只是想在不加载插件等的情况下尽可能地保持这一重量。它只是想在不加载插件等的情况下尽可能地保持这一重量。它只是想尽可能地保持这一重量没有加载插件等。。