日期格式中的年份的周数 [英] Week number of a year in dateformat

查看:780
本文介绍了日期格式中的年份的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用格式 YYYY 和月 MM 等等。我正在寻找是否有格式来获取一年的周数。

I need to get the year I use the format YYYY, and month MM, and so on. I am looking to see if there is a format to get the week number of the year.

我使用的例子的日期是 2008 -03-09 16:05:07.000 ,在这种情况下我想知道 11 的星期编号。有没有办法以编程方式检索 11

The date that I am using as example is 2008-03-09 16:05:07.000, and I would like to know the week number which is 11 in this case. Is there a way to retrieve 11 programmatically?

推荐答案

它提供了周数,但你可以很容易地得到它:

There's no format that provides the weeknumber, but you can get it in this way easily:

System.Globalization.CultureInfo cul = System.Globalization.CultureInfo.CurrentCulture;
int weekNum = cul.Calendar.GetWeekOfYear(
    DateTime.Now, 
    System.Globalization.CalendarWeekRule.FirstFourDayWeek, 
    DayOfWeek.Monday);




  • CultureInfo.Calendar 属性

  • Calendar.GetWeekOfYear 方法

  • 自定义日期和时间格式字符串

    • CultureInfo.Calendar Property
    • Calendar.GetWeekOfYear Method
    • Custom Date and Time Format Strings
    • 回答您的评论如何获取给定的 DateTime

      To answer your comment how to get the quarter of a year of a given DateTime:

      int quarter = (int)((DateTime.Now.Month - 1) / 3) + 1
      

      这篇关于日期格式中的年份的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆