使用 luxon 时遇到问题,计算两个日期之间的时差

问题描述

StackOverflow 的好心人,我在使用 luxon 时遇到了问题(我想是我设置错误或做错了什么),现在它无法计算未来日期(如 NaN),以及过去日期将年数考虑在内,这样你就会得到这样的结果:

enter image description here

enter image description here

[

enter image description here

enter image description here

我希望这段代码做的是专注于月和日(忘记年份),如果过去的日期距离今天的日期有 7 天或不到 7 天,请说“通过的日期是

代码

const isBirthdayThisWeek = (birthDate) => {
  const endDate = DateTime.Now()
  const startDate = DateTime.fromISO(birthDate)
  const interval = Interval.fromDateTimes(startDate,endDate)
  const dateDifference = interval.length('days')
  const wholeNumberedDateDifference = Math.round(dateDifference)
  wholeNumberedDateDifference <= 7
    ? console.log('bday is in less than a week',wholeNumberedDateDifference)
    : wholeNumberedDateDifference > 7
      ? console.log('bday is more than in a week',wholeNumberedDateDifference)
      : console.log('something went wrong',wholeNumberedDateDifference)
}

先谢谢大家。

解决方法

您可以使用计算持续时间的大小。持续时间适用于未来和过去的两个时间间隔。将开始日期调整为“对齐”到同一当前年份,以便日期差异不会跨越年份边界。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net.Configuration;
using System.Collections.Specialized;
using System.Configuration;
using System.Net;
using System.Collections;
using System.Net.Security;


namespace Assignment
{
    public partial class push_notification_admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender,EventArgs e)
        {

        }

        protected void Button1_Click(object sender,EventArgs e)
        {
            string strcon = ConfigurationManager.ConnectionStrings["Database1ConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand("SELECT * from Notification",con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlDataReader reader;
            con.Open();
            reader = cmd.ExecuteReader();
            ArrayList emailArray = new ArrayList();

            SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");

            while (reader.Read()){
                emailArray.Add(reader["Notice_email"].ToString());
            }
            foreach (string email in emailArray) {

                using (MailMessage mm = new MailMessage(smtpSection.From,"sender@gmail.com"))
                {
                    // Gmail Address from where you send the mail
                    var fromAddress = "inpuzzle2021@gmail.com";

                    // any address where the email will be sending
                    var toAddress = email;

                    //Password of your gmail address
                    const string fromPassword = "inpuzzlepwd";

                    // Passing the values and make a email formate to display
                    string subject = TextBox1.Text.ToString();
                    string body = TextBox2.Text.ToString();

                    // smtp settings
                    var smtp = new System.Net.Mail.SmtpClient();
                    {
                        smtp.Host = "smtp.gmail.com";
                        smtp.Port = 587;
                        smtp.EnableSsl = true;
                        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential(fromAddress,fromPassword);
                        smtp.Timeout = 20000;
                    }

                    // Passing values to smtp object
                    smtp.Send(fromAddress,toAddress,subject,body);
                }
            }
            reader.Close();
            con.Close();
        }
    }
}

输出

import { DateTime } from "luxon";

const startDate = DateTime.fromISO("2000-04-26").set({
  year: DateTime.now().get("year")
});
const diff = Math.abs(startDate.diffNow().as('day'));
const wholeNumberedDateDifference = Math.round(diff);
if (diff <= 7) {
  console.log("bday is in less than a week",wholeNumberedDateDifference);
} else {
  console.log("bday is more than in a week",wholeNumberedDateDifference);
}

演示

Edit having-trouble-with-luxon-calculating-time-difference-between-2-dates