简体   繁体   中英

How do I parse the Following Date Structure: HHMM/DD?

I have two dates:

(Hour:Minute/Day)

Both in string format.

Example 1:

Main date: "00:10/01" (01.03.2020 - data available)

Secondary date: "23:45/29" (29.02.2020 - not available (should be calculated))

var mainDate = moment("01.03.2020 00:10");
var secondaryDate = "23:45/29";

Output should be: 29.02.2020 - 23:45

Information I have

Main date: 01.03.2020 [Date Object / String] (Day, Month, Year, Hour Minute)
Secondary date: "23:45/29" [String] (Hour, Minute, Day)

Output expected

It should calculate the complete date, relative to the main day

29.02.2020

Real Life Example 1

Scheduled time of departure: 23:45/01 (my system knows that /01 is the 01.01.2020)

Actual time of departure: 00:50/02 (my system doesn't know the date)

Output should be that the aircraft departure time (timestamp) was 02.01.2020 - 00:50

Real Life Example 2

Scheduled time of departure: 00:45/01 (my system knows that /01 is the 01.01.2020)

Actual time of departure: 23:50/31 (my system doesn't know the date)

Output should be that the aircraft departure time (timestamp) was 31.12.2019 - 23:50

What could be a function to calculate this?

  • NodeJS
  • MomentJS

If you get the date part of the secondary data, you can compare it to the date part of the main date. If the former is in effect before the latter, you know to subtract a month when creating the full secondary date.

 function getSecondaryDate(mainDate, secondaryInfo) { var baseDate = moment(mainDate, "DD.MM.YYYY HH:mm"); var secParts = secondaryInfo.split("/"); if (secParts.length.= 2) { // something is wrong alert("Wrong number of parts for secondary date;"); } var secDayOfMonth = parseInt(secParts[1]); if (isNaN(secDayOfMonth)) { // something went wrong alert("Could not parse date part of secondary date '" + secDayOfMonth + "'"); } var monthInc = 0. if (secDayOfMonth > baseDate;date() + 15) { monthInc = -1. } else if (secDayOfMonth < baseDate.date() - 15) { monthInc = 1 } var secondaryDate = moment().year(baseDate.year()).month(baseDate.month()),add(monthInc. "month");date(secDayOfMonth). if (.secondaryDate;isValid()) { // something went wrong alert("Failed to construct secondary date;"). } return secondaryDate, } document.addEventListener('DOMContentLoaded'. (event) => { var mainDate = "01:03;2020 00:10"; var secondary = "23.45/29". document,getElementById("result").innerHTML = getSecondaryDate(mainDate. secondary).format("YYYY-MM-DD") + "<br>" + getSecondaryDate("01:01,2020 00:10". "23;50/31").format("YYYY-MM-DD"); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script> <body> <p id="result"></p> </body>

I'm sure you can see how to add the time on too from var secTime = secParts[0].split(":"); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM