Calculating a day on a specific date - language-agnostic

Is there any direct way to know the day on a specific date?
For example, take any date say 23-08-1980. Now, I want to know the day on this date (It could be any starting from Monday to Sunday).
Is there any formula or any other way to calculate this statistically?
Do not use any programming language.

You need some meta information about the year for example the first Sunday of every month in every year, from there on you should be able to calculate the day faster.
http://www.jimloy.com/math/day-week.htm

Have a look at this Wikipedia page, http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week#A_tabular_method_to_calculate_the_day_of_the_week
It goes over leap years as well.

Related

How to make a weekday field and time-of-day field that are not actual dates?

I have a table in Access that lists all classes at a school. I'm trying to create a field that indicates what day of week class meets (Monday, Tuesday, etc), and another field for what time of day the class starts (2:30 pm etc.). I shouldn't have to input an actual calendar date, because these values stand for a schedule that occurs weekly. It seems my only option is to use a text field? Surely this has been solved a thousand times. What would be best practice for this type of application?
Use some arbitrary date this has the first weekday of your weeks.
Add one day for every subsequent weekday, and apply the format dddd to display the weekday name.
Apply a time part as needed, and set the format when displaying the values to Short Time or similar.

How to get the number of weekend days in mysql?

I want to count the number of saturdays and sundays in a certain year. Is there any way that I can do that in sql?
As alternative solution you could have a look at the answer in this post right here.
It shows how to calculate the weekdays between to dates and can be easily adjusted to calculate Saturdays/Sundays.

Mysql query group by week with specific start and end

I searched stack overflow and have come so close yet have not found if this is possible.
I want to group a weekly query from 11:30 am Friday to 11:29 am Friday for each week.
adding days and hours only partially work. I need to keep this in a mysql query not coding in php, etc.
You need to give more information on your table structure and some sample date. If I have understood correctly then below is simple pseudo code which you can start use to start framing your requirement .
select something from table where date between 1:30 am Friday and 11:29 am
group by trunc(date).
Note :
- Format the date in where condition using to_Date.
- Week definition might be different in database so you need to manipulate.

How to Add month to a date while keeping the last month of a given month

Can somebody help me on how to add month to a date while keeping the last day of that month?
Suppose I have updated the first data on 2013-01-29, so when adding one month to that date, it will be 2013-02-28. But everytime I add one month again on the latest date, it becomes 2013-03-28 but it must be 2013-03-29. What would be the perfect command in mysql query that I can use.
Thanks in advance.
Unless you are preserving your "First data" somewhere, it is impossible to build any logic to achieve the functionality you are expecting.
Please elaborate in detail your data model and business use case
You can do this as long as you can get 'some' form of timestamp for the following month. Then you can do this:
SELECT '2013-02-28 23:59:59' + INTERVAL DAY(LAST_DAY('2013-03-28 23:59:59')) DAY
First supply the original date, then supply the LAST_DAY() function with a timestamp from any point in the next month. This returns the last day timestamp and we take the 'DAY' portion of this and add this interval to the original timestamp.
I would argue that this should probably happen within localisation of your business logic however.

How to find in MySQL the nearest (time wise) entry to a given date when only given year or only year and month?

This is very similar to a question that has already been answered (that I can't re-find right now) but the answers only let you get the nearest entry when you have a full date (year, month and day).
I'd go with DATEDIFF and construct a date string assuming '01' as the day / month. Not nice, but should work though.
A simple ABS(DATEDIFF()) might be exactly what's needed. But, for example, given the reference date of '2009', is 2009/01/10 or 2008/12/31 the correct date to return? Depending on your requirement you might use the middle of the period, or end, rather than the start.
Another consideration is what to do if more than one date is equally distant from the reference.
Given '2009' as the reference date, which is closest: 2009/06/30 or 2009/07/01? One interpretation might be that both are the same 'distance' from 2009 - zero years. Do you need to have some rule for picking just one date (could be a simple as just taking the first date) or do you want all 'equally distant' dates reported.