如何在javascript中格式化此日期/时间?
我如何转换如何在javascript中格式化此日期/时间?,javascript,Javascript,我如何转换 2018-05-07T17:51:17.258000-07:00 进入: 使用javascript 我对javascript完全陌生,无法理解这一点。您可以使用类似于Moment.js的库: 您可以使用Moment.js这样的库: 您可以使用现代浏览器中提供的选项 您所需的格式对应于加拿大地区,该地区通过在“p.m.”中添加点并在小写字母中添加“PM”来区别于美国地区: var someDate=新日期(); var str=someDate.tolocalString('en-
2018-05-07T17:51:17.258000-07:00
进入:
使用javascript
我对javascript完全陌生,无法理解这一点。您可以使用类似于Moment.js的库:
您可以使用Moment.js这样的库: 您可以使用现代浏览器中提供的选项 您所需的格式对应于加拿大地区,该地区通过在“p.m.”中添加点并在小写字母中添加“PM”来区别于美国地区:
var someDate=新日期();
var str=someDate.tolocalString('en-CA',{
月:“长”,
日期:“数字”,
年份:“数字”,
小时:“数字”,
分钟:“两位数”
});
console.log(str)代码>可以与现代浏览器中可用的选项一起使用
您所需的格式对应于加拿大地区,该地区通过在“p.m.”中添加点并在小写字母中添加“PM”来区别于美国地区:
var someDate=新日期();
var str=someDate.tolocalString('en-CA',{
月:“长”,
日期:“数字”,
年份:“数字”,
小时:“数字”,
分钟:“两位数”
});
console.log(str)代码>有很多选择
(简单)使用JavaScript库
退房
他们有一个.format方法,非常容易使用
moment('2018-05-07T17:51:17').format('MMM DD, YYYY, h:mm a')
(简单)使用系统日期格式
您可以选择使用.toLocaleString()而不是使用自定义格式,该格式将按照运行机器的语言设置来格式化日期
new Date('2018-05-07T17:51:17').toLocaleString();
(复杂)用普通JavaScript编写自定义格式方法
JS本身在处理日期方面相当复杂,但绝对有可能。建议您阅读Date对象()上所有方法的文档,因为有些方法返回的结果可能与您刚接触JavaScript时预期的结果略有不同
看看这把小提琴:
我添加了一些注释来解释我所做的事情
const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
document.body.innerHTML = getFormattedDate(new Date('2018-05-07T17:51:17'));
/**
* Get the formatted date
* @param {Date} date
* @returns {String}
*/
function getFormattedDate(date) {
const month = getMonthName(date);
const day = getNumericDay(date);
const year = getYear(date);
let hour = (date.getHours() + 12) % 12;
if (hour === 0) {
hour = 12;
}
let minute = date.getMinutes();
if (minute.length === 1) {
minute = `0${minute}`;
}
const isAm = date.getHours() < 12;
return `${month} ${day}, ${hour}:${minute} ${isAm ? 'a.m.' : 'p.m.'}`;
}
/**
* Get the name of the month
* @param {Date} date
* @returns {String}
*/
function getMonthName(date) {
// getMonth returns 0-based value
const zeroBasedMonthInt = date.getMonth();
return MONTHS[zeroBasedMonthInt];
}
/**
* Get number value of the day of the month of a date
* @param {Date} date
* @returns {Number}
*/
function getNumericDay(date) {
// .getDay returns zero-based day in week (starting from Sunday)
// .getDate returns day of month (starting with 1)
return date.getDate();
}
/**
* Get the full year of a date
* @param {Date} date
* @returns {Number}
*/
function getYear(date) {
// getYear returns year since 1900
// getFullYear returns year (starting from 0)
return date.getFullYear();
}
const MONTHS=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月];
document.body.innerHTML=getFormattedDate(新日期('2018-05-07T17:51:17');
/**
*获取格式化的日期
*@param{Date}Date
*@返回{String}
*/
函数getFormattedDate(日期){
const month=getMonthName(日期);
const day=getNumericDay(日期);
const year=getYear(日期);
设小时=(date.getHours()+12)%12;
如果(小时==0){
小时=12;
}
让minute=date.getMinutes();
如果(分钟长度===1){
分钟=`0${minute}`;
}
const isAm=date.getHours()<12;
返回`${month}${day},${hour}:${minute}${isAm?'a.m.:'p.m.}`;
}
/**
*获取月份名称
*@param{Date}Date
*@返回{String}
*/
函数getMonthName(日期){
//getMonth返回基于0的值
const zeroBasedMonthInt=date.getMonth();
返回月份[zeroBasedMonthInt];
}
/**
*获取日期的月份中的日期的数值
*@param{Date}Date
*@返回{Number}
*/
函数getNumericDay(日期){
//.getDay返回一周中基于零的日期(从星期日开始)
//.getDate返回月的第几天(从1开始)
返回日期。getDate();
}
/**
*得到一整年的约会
*@param{Date}Date
*@返回{Number}
*/
功能getYear(日期){
//getYear返回自1900年以来的年份
//getFullYear返回年份(从0开始)
返回日期。getFullYear();
}
有很多选择
(简单)使用JavaScript库
退房
他们有一个.format方法,非常容易使用
moment('2018-05-07T17:51:17').format('MMM DD, YYYY, h:mm a')
(简单)使用系统日期格式
您可以选择使用.toLocaleString()而不是使用自定义格式,该格式将按照运行机器的语言设置来格式化日期
new Date('2018-05-07T17:51:17').toLocaleString();
(复杂)用普通JavaScript编写自定义格式方法
JS本身在处理日期方面相当复杂,但绝对有可能。建议您阅读Date对象()上所有方法的文档,因为有些方法返回的结果可能与您刚接触JavaScript时预期的结果略有不同
看看这把小提琴:
我添加了一些注释来解释我所做的事情
const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
document.body.innerHTML = getFormattedDate(new Date('2018-05-07T17:51:17'));
/**
* Get the formatted date
* @param {Date} date
* @returns {String}
*/
function getFormattedDate(date) {
const month = getMonthName(date);
const day = getNumericDay(date);
const year = getYear(date);
let hour = (date.getHours() + 12) % 12;
if (hour === 0) {
hour = 12;
}
let minute = date.getMinutes();
if (minute.length === 1) {
minute = `0${minute}`;
}
const isAm = date.getHours() < 12;
return `${month} ${day}, ${hour}:${minute} ${isAm ? 'a.m.' : 'p.m.'}`;
}
/**
* Get the name of the month
* @param {Date} date
* @returns {String}
*/
function getMonthName(date) {
// getMonth returns 0-based value
const zeroBasedMonthInt = date.getMonth();
return MONTHS[zeroBasedMonthInt];
}
/**
* Get number value of the day of the month of a date
* @param {Date} date
* @returns {Number}
*/
function getNumericDay(date) {
// .getDay returns zero-based day in week (starting from Sunday)
// .getDate returns day of month (starting with 1)
return date.getDate();
}
/**
* Get the full year of a date
* @param {Date} date
* @returns {Number}
*/
function getYear(date) {
// getYear returns year since 1900
// getFullYear returns year (starting from 0)
return date.getFullYear();
}
const MONTHS=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月];
document.body.innerHTML=getFormattedDate(新日期('2018-05-07T17:51:17');
/**
*获取格式化的日期
*@param{Date}Date
*@返回{String}
*/
函数getFormattedDate(日期){
const month=getMonthName(日期);
const day=getNumericDay(日期);
const year=getYear(日期);
设小时=(date.getHours()+12)%12;
如果(小时==0){
小时=12;
}
让minute=date.getMinutes();
如果(分钟长度===1){
分钟=`0${minute}`;
}
const isAm=date.getHours()<12;
返回`${month}${day},${hour}:${minute}${isAm?'a.m.:'p.m.}`;
}
/**
*获取月份名称
*@param{Date}Date
*@返回{String}
*/
函数getMonthName(日期){
//getMonth返回基于0的值
const zeroBasedMonthInt=date.getMonth();
返回月份[zeroBasedMonthInt];
}
/**
*获取日期的月份中的日期的数值
*@param{Date}Date
*@返回{Number}
*/
函数getNumericDay(日期){
//.getDay返回一周中基于零的日期(从星期日开始)
//.getDate返回月的第几天(从1开始)
返回日期。getDate();
}
/**
*得到一整年的约会
*@param{Date}Date
*@返回{Number}
*/
功能getYear(日期){
//getYear返回自1900年以来的年份
//getFullYear返回年份(从0开始)
返回日期。getFullYear();
}
我现在添加了修复我现在添加了修复