-1

How can I get the day (e.g Friday) from given date, without using DateTimePicker or other calender types of tools or built-in functions?

I want to implement a get_Day(desired_Date) function by providing it the date.

The function is provided a date as "dd-mmm-yyyy" format, and it is requested to return a string for the day for example. I provide 11-Sep-2014, then the function should return Thursday as string.

Can anyone help me regarding a mathematical formula to do this?

Waqas Shabbir
  • 755
  • 1
  • 14
  • 34
  • 2
    And what do you mean by day? Thursday? or 11th? Both are supported by DateTime, either DayOfWeek or Day properties. – Lasse V. Karlsen Sep 11 '14 at 09:35
  • 2
    Please select one language, however the solution will be the languages C# and C are so totally different that any solution will most likely not be portable between them. – Some programmer dude Sep 11 '14 at 09:40
  • Either C, C++ or C#. I want just a Mathematical format. – Waqas Shabbir Sep 11 '14 at 09:44
  • 2
    There is no "formula". Leap years ruin it – duffymo Sep 11 '14 at 09:46
  • @duffymo, if ther is not a formula, then how can the Built-In function calculate it? – Waqas Shabbir Sep 11 '14 at 09:48
  • The built in function has a rather large database and a lot of code to do it. How to do it also varies with location, different places have had a tendency to switch calendars and timezones etc, so it is far from as easy as only knowing all the leap year rules. – perh Sep 11 '14 at 09:49
  • You have access to Java JDK source code. Go look. – duffymo Sep 11 '14 at 09:49
  • Which calendar? I assume Gregorian? Which other assumptions do you have? Ie limitations in your assignment, please state them. – flindeberg Sep 11 '14 at 09:54
  • Yes Georgian Calender. – Waqas Shabbir Sep 11 '14 at 09:56
  • 1
    Please edit your **question** to reflect the complete requirement, i.e. input value, desired output, qualifying criteria. Don't assume that everyone lives in your time zone and knows what you mean when you say "day". – teylyn Sep 11 '14 at 09:59
  • @teylyn it is clearly mention in the question that what is meant by day... I mean Friday, Saturday or other day. – Waqas Shabbir Sep 11 '14 at 10:03
  • possible duplicate of [C Program to find day of week given date](http://stackoverflow.com/questions/6054016/c-program-to-find-day-of-week-given-date) – Sagar Pilkhwal Sep 11 '14 at 10:04
  • @WaqasShabbir Your question is not clear. You need to be more specific. Otherwise there would not be so many comments asking for clarification. – teylyn Sep 11 '14 at 10:16

2 Answers2

0

The formula used by Microsoft on their DateTime.DayOfWeek C# property is:

(Ticks / 864000000000L + 1L) % 7L

Example of usage:

DateTime samp = new DateTime(2013, 05, 18);
Console.WriteLine(string.Format("Ticks: {0}", samp.Ticks));
Console.WriteLine(string.Format("The day of the week as a number: {0}", (samp.Ticks / 864000000000L + 1L) % 7L)); // The formula they use to calculate day of week
Console.WriteLine(samp.DayOfWeek);
Console.ReadLine();

The definition of a tick: http://msdn.microsoft.com/en-us/library/system.datetime.ticks%28v=vs.110%29.aspx

Bernd Linde
  • 2,098
  • 2
  • 16
  • 22
0

Here is one way to do it.

#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */

int main ()
{
  time_t timer;

  // get time now
  timer = time(NULL);

  // Our number is the difference between now and 1st Jan 1970 00:00 - our reference time
  printf ("%u seconds have passed since since January 1, 1970\n", timer);

  // print number of days since reference time
  time_t days = timer / (60 * 60 * 24);
  printf ("%u days have passed since since January 1, 1970\n", days);

  // January 1, 1970 is a Thursday - so we have to cater for that.  Easiest is to say Thursday is our reference zero.
  // modulo arithmetic to get unadjusted day of week
  int ref_thurs_day = days % 7;
  printf ("day of week reference to thursday is: %u\n", ref_thurs_day);

  switch(ref_thurs_day) {
    case 0: puts("Thursday"); break;
    case 1: puts("Friday"); break;
    case 2: puts("Saturday"); break;
    case 3: puts("Sunday"); break;
    case 4: puts("Monday"); break;
    case 5: puts("Tuesday"); break;
    case 6: puts("Wednesday"); break;
    default: puts("Something went wrong!");
  }

  return 0;
}
Angus Comber
  • 9,316
  • 14
  • 59
  • 107