Solved

DateTime Compare Function

Posted on 2008-06-20
4
1,679 Views
Last Modified: 2013-12-16
hi,
I have the following function that returns always 1 no matter what:
for (i = 0; i <= Count - 2; i++) {
            for (j = 0; j <= 6; j++) {                
                 strWeekDay = DateTime.Today.AddDays(DateTime.Compare(DateTime.Today.AddDays(j), StartTime)).ToString("dddd");
...etc
  }
}
When I run it (Today) it always return Saturday!. I tried the compare function on its own and namtter what I enter for the StartTime (DateTime Variable) it always returns 1

I am trying to get the difference between the two sates in Days of the Week (Sunday, Monday...)

any idea how to fix it?
I am using ASP.NET - C#
0
Comment
Question by:smaguire
  • 2
4 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 21834270
Can't you use DateTime.Compare() ?
0
 

Author Comment

by:smaguire
ID: 21834288
thats what I am using
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 21834507
are you trying to calculate the Day of Week?
There is a property for this:

int dow = (int)(yourDateTime.DayOfWeek);

also, when comparing, consider you are comparing date and time, this can give you unexpected results, if you want to compare just dates, do something like this:

datetime1.Date.Compare(datetime2.Date)
 
0
 

Author Closing Comment

by:smaguire
ID: 31469301
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now