Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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:

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


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

Edit: Code formatted.
Posted
Updated 8-Sep-11 18:27pm
v4
Comments
BillWoodruff 8-Sep-11 23:22pm    
There certainly is this functionality in C# ! Why haven't you looked at the DateTime Type in C#, and examined its methods ?

I suggest that you take a look here[^]. This is the documentation for the System.DateTime class, which includes methods for manipulating dates.
 
Share this answer
 
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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900