C#DatePart()? [英] C# DatePart() ?

查看:64
本文介绍了C#DatePart()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我正在将一些VB类的方法转换为另一个

项目的C#类。这很容易,但是当转换方法返回最后一个

周数时,我遇到了问题。 VB代码是...


私有函数GetLastWeek(ByVal year As Integer)作为整数

''//获取一年中的最后一天

Dim dte As DateTime = New DateTime(年,12,31)

''//返回一年中最后一天的周数

返回dte。年* 100 + DatePart(DateInterval.WeekOfYear,dte,

FirstDayOfWeek.Monday,FirstWeekOfYear.System)

结束功能


如何使用C#执行此操作,因为下面的代码不起作用

因为DatePart不适用于C#?


private int GetLastWeek(int year)

{

//获取一年中的最后一天

DateTime d = new DateTime(年,12,31);

//返回一年中最后一天的周数

返回d.Year * 100 + DatePart(DateInterval.WeekOfYear,d,

FirstDayOfWeek.Monday,FirstWeekOfYear.System);

}


- -

提前致谢!


Mika

解决方案

在星期二, 2007年6月5日00:37:50 -0700,Mika M< ma *************** @ luukku.com

写道:
< blockquote class =post_quotes>
[...]

如何使用C#执行此操作,因为下面的代码不起作用

因为DatePart ;在C#中不可用?



我认为你正在寻找Calendar课程。例如:


private int GetLastWeek(int year)

{

//获取一年中的最后一天
DateTime d = new DateTime(年,12,31);

//返回一年中最后一天的周数

返回d.Year * 100 +

GregorianCalendar.GetWeekOfYear(d,

DateTimeFormatInfo.Current.CalendarWeekRule,DayOfWeek.Monday);

}


以上明确使用公历,但如果您因某种原因需要处理另外的

日历,您也可以使用

CurrentCulture.Calendar。


Pete


6月5日上午9:42,Peter Duniho < NpOeStPe ... @nnowslpianmk.com>

写道:


2007年6月5日星期二00:37:50 -0700 ,Mika M< mahmik_removet ... @ luukku.com>

写道:


[...]

如何使用C#执行此操作,因为下面的代码不起作用

因为DatePart在C#中不可用?



我认为你正在寻找Calendar课程。例如:


private int GetLastWeek(int year)

{

//获取一年中的最后一天
DateTime d = new DateTime(年,12,31);

//返回一年中最后一天的周数

返回d.Year * 100 +

GregorianCalendar.GetWeekOfYear(d,

DateTimeFormatInfo.Current.CalendarWeekRule,DayOfWeek.Monday);

}



小修正 - GetWeekOfYear并非静态,并且DateTimeFormatInfo中没有当前

属性:


private static int GetLastWeek(int year)

{

//获取一年中的最后一天

DateTime d = new DateTime(year,12, 31);

//返回一年中最后一天的周数

GregorianCalendar calendar = new GregorianCalendar();

return d.Year * 100 +

calendar.GetWeekOfYear(d,

DateTimeFormatInfo.CurrentInfo.Calen darWeekRule,

DayOfWeek.Monday);

}


Jon


2007年6月5日星期二02:02:24 -0700,Jon Skeet [C#MVP]< sk *** @ pobox.com>

写道:


小修正 - GetWeekOfYear不是静态的,并且DateTimeFormatInfo中没有Current

属性:



教我这么晚才发帖。谢谢。 :)


Hi!

I''m converting some methods of VB-class into C#-class for another
project. It''s quite easy, but when converting method which returns last
week number of the entered year I got problems. The VB code is...

Private Function GetLastWeek(ByVal year As Integer) As Integer
''// Get last day of the year
Dim dte As DateTime = New DateTime(year, 12, 31)
''// Return last day weeknumber of the year
Return dte.Year * 100 + DatePart(DateInterval.WeekOfYear, dte,
FirstDayOfWeek.Monday, FirstWeekOfYear.System)
End Function

How to do this using C# because the following code below is not working
because "DatePart" is not available in C#?

private int GetLastWeek(int year)
{
// Get last day of the year
DateTime d = new DateTime(year, 12, 31);
// Return last day weeknumber of the year
return d.Year * 100 + DatePart(DateInterval.WeekOfYear, d,
FirstDayOfWeek.Monday, FirstWeekOfYear.System);
}

--
Thanks in advance!

Mika

解决方案

On Tue, 05 Jun 2007 00:37:50 -0700, Mika M <ma***************@luukku.com
wrote:

[...]
How to do this using C# because the following code below is not working
because "DatePart" is not available in C#?

I think you are looking for the Calendar class. For example:

private int GetLastWeek(int year)
{
// Get last day of the year
DateTime d = new DateTime(year, 12, 31);
// Return last day weeknumber of the year
return d.Year * 100 +
GregorianCalendar.GetWeekOfYear(d,
DateTimeFormatInfo.Current.CalendarWeekRule, DayOfWeek.Monday);
}

The above explicitly uses the Gregorian calendar, but you could also use
CurrentCulture.Calendar if you wanted for some reason to handle alternate
calendars.

Pete


On Jun 5, 9:42 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

On Tue, 05 Jun 2007 00:37:50 -0700, Mika M <mahmik_removet...@luukku.com>
wrote:

[...]
How to do this using C# because the following code below is not working
because "DatePart" is not available in C#?


I think you are looking for the Calendar class. For example:

private int GetLastWeek(int year)
{
// Get last day of the year
DateTime d = new DateTime(year, 12, 31);
// Return last day weeknumber of the year
return d.Year * 100 +
GregorianCalendar.GetWeekOfYear(d,
DateTimeFormatInfo.Current.CalendarWeekRule, DayOfWeek.Monday);
}

Small corrections - GetWeekOfYear isn''t static, and there''s no Current
property in DateTimeFormatInfo:

private static int GetLastWeek(int year)
{
// Get last day of the year
DateTime d = new DateTime(year, 12, 31);
// Return last day weeknumber of the year
GregorianCalendar calendar = new GregorianCalendar();
return d.Year * 100 +
calendar.GetWeekOfYear(d,
DateTimeFormatInfo.CurrentInfo.CalendarWeekRule,
DayOfWeek.Monday);
}

Jon


On Tue, 05 Jun 2007 02:02:24 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:

Small corrections - GetWeekOfYear isn''t static, and there''s no Current
property in DateTimeFormatInfo:

Teach me to post so late. Thanks. :)


这篇关于C#DatePart()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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