DateTime数据类型的算术运算. [英] Arithmetic Operations on DateTime Datatype.

查看:126
本文介绍了DateTime数据类型的算术运算.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在尝试寻找一种对C#DateTime数据类型变量进行算术运算的方法,但是我什么也没得到,
我的要求是从给定的Date(//Monday = 0)生成工作日索引
在VBA中,我可以直接通过以下方式获取此信息:

Hi,

I have been trying to find a way to do Arithmetic operations on C# DateTime datatype variables but I couldn''t get any,
My Requirement is to Generate weekday index from given Date(//Monday =0)
in VBA I can get this Directly By:

Dim WeekDay as Integer
Dim NewYear As Date
NewYear = DateSerial(2009, 1, 1)
WeekDay = NewYear Mod 7  



但是C#中没有提供此类功能,无法对带有其他数据类型变量的DateTime变量进行操作.

有人知道吗,它在C#中提供了吗?!?为什么没有?

-特里纳德

代码格式化.



But There is no such functionality provided in C#, to do operations on DateTime variable with Other Datatype variable.

Is anybody have idea, is it provided in C#!!?, why it is not?

-Trinadh

Code formatted.

推荐答案

我建​​议您看看
I suggest that you take a look here[^]. This is the documentation for the System.DateTime class, which includes methods for manipulating dates.


DateSerial不属于C#.NET.原因是:

1.最初,DateSerial是一个VBScript函数,称为:
http://www.w3schools.com/vbscript/func_dateserial.asp [
DateSerial is not part of C# .NET. Reasons are:

1. Originally DateSerial is a VBScript function as referred:
http://www.w3schools.com/vbscript/func_dateserial.asp[^]

2. The 5 digit DateSerial value is specific to Excel; I know of no class in the .NET API that is going to provide the same value.

Still, itz achievable through Excel''s DateValue access through C# using .NET Excel Interop funtionality.


您是否尝试过DayOfWeek属性? http://msdn.microsoft.com/en-us/library/system.datetime. dayofweek.aspx [ ^ ]

它返回枚举,可以轻松地将其转换为int.唯一的问题是,它从周日= 0开始,但是如果您要从星期一开始,我可以减去1并将-1替换为6.
Have you tried DayOfWeek property? http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx[^]

It returns enum, which can easily be converted to int. The only problem is that it starts with Sunday = 0, but I can substract 1 and replace -1 with 6 if you want to start from Monday.


这篇关于DateTime数据类型的算术运算.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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