c# - 如何使用日期格式获取每月的第一个星期日?
问题描述
嗨,我需要检查该月第一个星期日的条件,以获取格式为 YYYYMMDD 的日期
var calDate = data.value; // example 20210502 is sunday
if (first Sunday of the month)
{
do this
}
else
{
do that
}
我需要检查每月第一个星期日的上述情况
解决方案
Split your problem in two:
- parse the string to a
DateTime
object
var date = DateTime.ParseExact(calDate, "yyyyMMdd", null);
- Check if the
DateTime
object refers to the first sunday in a month. For this, it must obviously be a Sunday and the day part must be in the range 1 to 7:
var isFirstSunday = date.DayOfWeek == DayOfWeek.Sunday && date.Day <= 7;
推荐阅读
- html - 如何从网站布局的某些区域阻止 AdSense 的自动广告
- javascript - 使用 ESM 在浏览器中动态或静态导入 json
- java - 如何在 REST Spring 中正确处理具有相互对象引用的请求正文?
- python - 阴影和阴影符号混合在符号表中
- javascript - 如何使用给定的 ID 快速重定向?
- python - 对特定列中的特定行求和
- c# - 如何将表单数据传递给 ViewModel 属性?
- php - 未捕获的错误:找不到类“MyApp\Chat”
- java - 在 Apache Beam PCollection 中使用 AutoValueSchema 会给出“RuntimeException:Creator 参数 arg0 与模式字段不对应”
- mysql - 简单的 SQL 查询但返回错误的结果