使用下面的这个小实用程序JavaScript函数,它可以将基本的Date字符串转换为JavaScript Date对象。
日期字符串可以是多种格式,如下所示......
stringToDate("17/9/2014", "dd/MM/yyyy", "/");
stringToDate("9/17/2014", "mm/dd/yyyy", "/");
stringToDate("9-17-2014", "mm-dd-yyyy", "-");
我希望做的是将其扩展为还允许有条件的字符串09:12:49
09:12:49am
,09:12:49pm
所以我的函数下面用这行创建Date对象...
var formatedDate = new Date(dateItems[yearIndex], month, dateItems[dayIndex]);
将不得不做类似......
的事情var formatedDate = new Date(dateItems[yearIndex], month, dateItems[dayIndex], hours, minutes, seconds, milliseconds);
仅在原始字符串中检测到hours, minutes, seconds, milliseconds
时才添加 // Convert Date String into JS Date Object
// Can parse multiple Date formats
// stringToDate("17/9/2014", "dd/MM/yyyy", "/");
// stringToDate("9/17/2014", "mm/dd/yyyy", "/");
// stringToDate("9-17-2014", "mm-dd-yyyy", "-");
function stringToDate(d, _format, _delimiter) {
if((d instanceof Date)){
return d;
}
var formatLowerCase = _format.toLowerCase();
var formatItems = formatLowerCase.split(_delimiter);
var dateItems = d.split(_delimiter);
var monthIndex = formatItems.indexOf("mm");
var dayIndex = formatItems.indexOf("dd");
var yearIndex = formatItems.indexOf("yyyy");
var month = parseInt(dateItems[monthIndex]);
month -= 1;
var formatedDate = new Date(dateItems[yearIndex], month, dateItems[dayIndex]);
return formatedDate;
}
。
此代码是否可以执行此任务?
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = "blob";
xhr.withCredentials = true;
xhr.onreadystatechange = function (){
if (xhr.readyState === 4) {
var blob = xhr.response;
FileSaver.saveAs(blob, filename);
}
};
xhr.send();
我知道MomentJS,DateJS,DateJS Evolved和SugarJS,但我正在尝试为小型实用程序库执行此操作。