如何在 C# 中以编程方式构建 ISO 周数数据表?

发布于 2024-12-24 01:14:26 字数 277 浏览 2 评论 0原文

有人知道如何构建一周的数据表吗?

我听说有很多类型的计算,公历等等...... 我的需求是 ISO 周号,绑定到数据表取决于周号。

数据表有 2 列:ISOWeekNo 和 WeekName

ISOWeekNo   WeekName
1           01 Jan 2012 To 07 Jan 2012
2           08 Jan 2012 To 14 Jan 2012

如何在 C# 中基于 ISO 周号以编程方式构建?

Anyone know how to built DataTable of week no?

I heard there has a lot of type of calculations for that, Gregorian or etc...
My needs are ISO Week No and bind to datatable depends on week no.

The DataTable has 2 columns : ISOWeekNo and WeekName

ISOWeekNo   WeekName
1           01 Jan 2012 To 07 Jan 2012
2           08 Jan 2012 To 14 Jan 2012

How do I build programatically in C# based on ISO Week No?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2024-12-31 01:14:26
// Set up DataTable
var dt = new DataTable();
var col1 = new DataColumn("ISOWeekNo", typeof(int));
var col2 = new DataColumn("WeekName", typeof(string));
dt.Columns.AddRange(new[] { col1, col2 });
dt.PrimaryKey = new[] { col1 };

// Start with the first day of this year and check each day until next year.
var day = new DateTime(DateTime.Now.Year, 1, 1);
while (day.Year == DateTime.Now.Year)
{
    var weekNum = DateTimeFormatInfo.InvariantInfo.Calendar.GetWeekOfYear(day, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
    if (!dt.Rows.Contains(weekNum))
        dt.Rows.Add(weekNum, string.Format("{0} to {1}", day.ToString("dd' 'MMM' 'yyyy"), day.AddDays(6).ToString("dd' 'MMM' 'yyyy")));
    day = day.AddDays(1);
}
// Set up DataTable
var dt = new DataTable();
var col1 = new DataColumn("ISOWeekNo", typeof(int));
var col2 = new DataColumn("WeekName", typeof(string));
dt.Columns.AddRange(new[] { col1, col2 });
dt.PrimaryKey = new[] { col1 };

// Start with the first day of this year and check each day until next year.
var day = new DateTime(DateTime.Now.Year, 1, 1);
while (day.Year == DateTime.Now.Year)
{
    var weekNum = DateTimeFormatInfo.InvariantInfo.Calendar.GetWeekOfYear(day, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
    if (!dt.Rows.Contains(weekNum))
        dt.Rows.Add(weekNum, string.Format("{0} to {1}", day.ToString("dd' 'MMM' 'yyyy"), day.AddDays(6).ToString("dd' 'MMM' 'yyyy")));
    day = day.AddDays(1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文