如何将字符串转换为JavaScript日期对象?
var st = "date in some format"
var dt = new Date();
var dt_st = // st in Date format, same as dt.
如何将字符串转换为JavaScript日期对象?
var st = "date in some format"
var dt = new Date();
var dt_st = // st in Date format, same as dt.
当前回答
Var a = "13:15" var b = toDate(a, "h:m") / /警报(b); document . write (b); 函数toDate(dStr, format) { var now = new Date(); If (format == "h:m") { now.setHours(下游。substr (0, dStr.indexOf(“:”))); now.setMinutes(dStr.substr(dStr.indexOf(":") + 1)); now.setSeconds (0); 现在返回; 其他} 返回“格式无效”; }
其他回答
使用这段代码:(我的问题已经用这段代码解决了)
function dateDiff(date1, date2){
var diff = {} // Initialisation du retour
var tmp = date2 - date1;
tmp = Math.floor(tmp/1000); // Nombre de secondes entre les 2 dates
diff.sec = tmp % 60; // Extraction du nombre de secondes
tmp = Math.floor((tmp-diff.sec)/60); // Nombre de minutes (partie entière)
diff.min = tmp % 60; // Extraction du nombre de minutes
tmp = Math.floor((tmp-diff.min)/60); // Nombre d'heures (entières)
diff.hour = tmp % 24; // Extraction du nombre d'heures
tmp = Math.floor((tmp-diff.hour)/24); // Nombre de jours restants
diff.day = tmp;
return diff;
}
还有一种方法是在格式字符串上构建一个带有命名捕获组的正则表达式,然后使用该正则表达式从日期字符串中提取日、月和年:
function parseDate(dateStr, format) { const regex = format.toLocaleLowerCase() .replace(/\bd+\b/, '(?<day>\\d+)') .replace(/\bm+\b/, '(?<month>\\d+)') .replace(/\by+\b/, '(?<year>\\d+)') const parts = new RegExp(regex).exec(dateStr) || {}; const { year, month, day } = parts.groups || {}; return parts.length === 4 ? new Date(year, month-1, day) : undefined; } const printDate = x => console.log(x ? x.toLocaleDateString() : x); printDate(parseDate('05/11/1896', 'dd/mm/YYYY')); printDate(parseDate('07-12-2000', 'dd-mm-yy')); printDate(parseDate('07:12:2000', 'dd:mm:yy')); printDate(parseDate('2017/6/3', 'yy/MM/dd')); printDate(parseDate('2017-6-15', 'y-m-d')); printDate(parseDate('2015 6 25', 'y m d')); printDate(parseDate('2015625', 'y m d')); // bad format
你可以试试这个:
function formatDate(userDOB) { const dob = new Date(userDOB); const monthNames = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]; const day = dob.getDate(); const monthIndex = dob.getMonth(); const year = dob.getFullYear(); // return day + ' ' + monthNames[monthIndex] + ' ' + year; return `${day} ${monthNames[monthIndex]} ${year}`; } console.log(formatDate('1982-08-10'));
新的日期(2000,10,1)将给你“星期三11月1日2000 00:00:00 GMT+0100 (CET)”
0表示month,表示1月
这个答案是基于卡塞姆的答案,但它也适用于两位数的年份。我对卡塞姆的答案进行了编辑,但以防它没有被批准,我还将这个作为单独的答案提交。
function stringToDate(_date,_format,_delimiter) {
var formatLowerCase=_format.toLowerCase();
var formatItems=formatLowerCase.split(_delimiter);
var dateItems=_date.split(_delimiter);
var monthIndex=formatItems.indexOf("mm");
var dayIndex=formatItems.indexOf("dd");
var yearIndex=formatItems.indexOf("yyyy");
var year = parseInt(dateItems[yearIndex]);
// adjust for 2 digit year
if (year < 100) { year += 2000; }
var month=parseInt(dateItems[monthIndex]);
month-=1;
var formatedDate = new Date(year,month,dateItems[dayIndex]);
return formatedDate;
}
stringToDate("17/9/14","dd/MM/yyyy","/");
stringToDate("17/9/2014","dd/MM/yyyy","/");
stringToDate("9/17/2014","mm/dd/yyyy","/")
stringToDate("9-17-2014","mm-dd-yyyy","-")