jquery 使用JavaScript从字符串中提取和解析日期的问题

n3schb8v  于 2023-08-04  发布在  jQuery
关注(0)|答案(2)|浏览(96)

我目前正在编写一个JavaScript函数,它从字符串中提取日期,然后解析它以设置日期过滤器的开始和结束日期。但是,我遇到了日期解析部分的问题。

$("#startDateF").data("initialValue", startDateString);

$("#endDateF").data("initialValue", endDateString);

字符串
这些行将用于一个reset按钮函数,该函数将检索这些值并将日期过滤器的当前值设置回initialValues。
代码如下:

var cDate = dText.match(/\d{2}-\d{2}-\d{4}/)[0];
console.log(cDate);
let initialStartDate = new Date(cDate);
let initialEndDate = new Date(initialStartDate.getFullYear(), initialStartDate.getMonth() + 1, initialStartDate.getDate());
startDateString = formatDate(initialStartDate);
endDateString = formatDate(initialEndDate);
$("#startDateF").data("initialValue", startDateString);
$("#endDateF").data("initialValue", endDateString);

function formatDate(date) {
    let day = ("0" + date.getDate()).slice(-2);
    let month = ("0" + (date.getMonth() + 1)).slice(-2);
    let year = date.getFullYear();
    return day + "-" + month + "-" + year;
}


我尝试使用 formatDate 函数来格式化日期,但它似乎无法正常工作。当我使用这个方法时,它将startDate和endDate设置为aN-aN-NaN。我希望startDate设置为cDate,endDate设置为相同的日期,但在下一个月。例如,cDate = 16-04-2023startDate = 16-04-2023 endDate = 16-05-2023*

aamkag61

aamkag611#

假设您的cDate是dd-mm-yyyy,那么我建议使用这个简化的脚本

const formatDate = d => d.toJSON().slice(0, 10).split('-').reverse().join('-')

const getDates = dText => {
  let initialStartDate = dText.match(/\d{2}-\d{2}-\d{4}/)[0]; // no validation
  const [dd, mm, yyyy] = initialStartDate.split("-");
  let initialEndDate = new Date(yyyy, mm, dd, 15, 0, 0, 0); // not subtracting 1 from the 0 based month gives us next month - ignoring the fact that 31st of Feb will be 2 or 3rd of March
  return {
    startDate: initialStartDate,
    endDate: formatDate(initialEndDate)
  }
};
let dateStrings = getDates("16-04-2023");
$("#startDateF").text(dateStrings.startDate); // change to data() after testing
$("#endDateF").text(dateStrings.endDate);

// test to show the rollover:
dateStrings = getDates("31-01-2020");
console.log(dateStrings)

dateStrings = getDates("31-02-2020"); // this is an invalid date, we do not handle that in the above script
console.log(dateStrings)

个字符

eoigrqb6

eoigrqb62#

除了解析initialStartDate变量时,您的代码没有任何问题。JavaScript中的Date构造函数不直接接受格式为'dd-mm-yyyy'的日期字符串。要从自定义日期字符串创建Date对象,您需要解析单独的日、月和年组件,并将它们作为参数以适当的顺序传递给构造函数。
基本上你可以加上这个

var day = parseInt(cDate.split('-')[0], 10);
var month = parseInt(cDate.split('-')[1], 10);
var year = parseInt(cDate.split('-')[2], 10);

let initialStartDate = new Date(year, month, day);

字符串
并且会很好的工作
这里是完整的代码tho

var cDate = dText.match(/\d{2}-\d{2}-\d{4}/)[0];
console.log(cDate);
var day = parseInt(cDate.split('-')[0], 10);
var month = parseInt(cDate.split('-')[1], 10);
var year = parseInt(cDate.split('-')[2], 10);

let initialStartDate = new Date(year, month, day);

let initialEndDate = new Date(initialStartDate.getFullYear(), initialStartDate.getMonth() + 1, initialStartDate.getDate());

startDateString = formatDate(initialStartDate);
endDateString = formatDate(initialEndDate);
$("#startDateF").data("initialValue", startDateString);
$("#endDateF").data("initialValue", endDateString);
function formatDate(date) {
    let day = ("0" + date.getDate()).slice(-2);
    let month = ("0" + (date.getMonth() + 1)).slice(-2);
    let year = date.getFullYear();
    return day + "-" + month + "-" + year;
}


希望有帮助:D

相关问题