|
Date, Time, and Calendar FunctionsThis page presents the way various systems handle dates. May you never have a bad date. | Topics this page: |
time.gov: official US Time |
Competing Date FormatsThere are several standards followed to format date/time:
|
PrinciplesIt is best practice to communicate and store timestamps in the GMT/UTC timezone. Timestamps obtained by the client from the server are then converted to local time. An example of the coding for ASP.NET is offered in this article: Rendering UTC time in javascript for ASP.NET |
Getting Today's DateJavaIn Java, to return the current date and time: private long _timestampIn;
_timestampIn = System.currentTimeMillis(); // ... work to be timed ...
_timestampOut = System.currentTimeMillis();
Oracle PL/SQLIn Oracle PL/SQL, to return the current date and time for date x in time zone y as it would be in time zone z:NEW_TIME(x,y,z) Microsoft SQLThe RawDate returned would have a value like "2008-12-31 23.59.58.211".SELECT GETDATE() AS RawDate Use date formatting functions to extract portions of the MS SQL current date:
SELECT DATEPART(DayofYear,GETDATE()) AS DayCount
Use date formatting functions to extract portions of the MS SQL current date: SELECT DATEDIFF(dd,'2008/12/24',GETDATE()) AS DaysToXmas Specify a number after a dot: SELECT DATEADD(hh.24,GETDATE()) AS 24HoursFromNow The CONVERT() function provides formats (such as 101) used here to create today's date in a text field: SELECT CONVERT(CHAR(10),CURRENT_TIMESTAMP,101) Format 120 returns the 8 character "12.59.59" time text as a string: SELECT substring(Convert(CHAR(19),CURRENT_TIMESTAMP,101),12,8) Format 120 returns the 5 character "12.59" time text truncated string: SELECT substring(Convert(CHAR(19),CURRENT_TIMESTAMP,101),12,5) To create a DateTime field from a formatted date, use: SELECT DATEADD(day,DATEDIFF(day,'20081028',CURRENT_TIMESTAMP),'20081028');
ASP ClassicIn ASP VBScript, to obtain a time stamp:
now() yields a date and time “1/19/2003 11:39:00 PM” More completely:
return d.getTime() timeStart = millTimeStamp(); // ... something to be timed ... timeStop = millTimeStamp() elaspedMS = timeStop - timeStart Response.write "Elapsed milliseconds=" & elapsedMS & elapsedpretty(elaspedMS) Other ASP date functions:
CDate() returns an expression that has been converted to a Variant of subtype Date. It recognizes date formats according to the locale setting of your system. The correct order of day, month, and year may not be determined if it is provided in a format other than one of the recognized date settings. In addition, a long date format is not recognized if it also contains the day-of-the-week string.
C# Timers.NET has static properties:
DateTime.Now returns the current time. In Excel VBA macros:
The Windows API method GetTickCount() retrieves times in 1ms (millisecond) resolution. But the "unmanaged" Win32 API method QueryPerformanceCounter() provides more precise values from high performance timers which runs at the 50,000 (or other) cycles per second LARGE_INTEGER reported by the QueryPerformanceFrequency() call. Daniel Strigl provides sample code to make use of these functions to calculate
Frequency is driven by microarchitecture (the number of gates per pipepline stage).
Rational Robot SQA VB ScriptIn Rational Robot SQA (vb) script:
Dim currentdate
PHP:
PerlTo get UT in Perl:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
$t = sprintf "%4d-%02d-%02dT%02d:%02dZ\n",
| Related: Javascript formatDate function ala PHP
|
Calendar Eras
Monks (are supposed to) focus on Christ. So the Gregorian calendar begins from zero in the "Christian era", around the time Jesus Christ was born. A "calendar era" is the fixed starting date for when a year numbering system begins. Examples:
BC/AD EraThe Gregorian calendar used in America and Europe is named after pope Gregory III. Popes speak Latin. So the"AD" after Gregorian year numbers signify Anno Domini (Latin for "in the year of our Lord", with Anno meaning "in the year" and Domini meaning "of our Lord". "BC" is an abbreviation for the English words "Before Christ". Designations used by other European languages include:
In the 1950's, academics who wanted a secular name (more politically correct to those who deny Christ) invented the word "Common Era" (abbreviated to "CE") for use instead of "AD", with "BCE" (for Before the Common Era) instead of "BC". The current Japanese calendar era began at Heisei 1 in early 1989 upon accession of the current Emperor to the throne. The Hebrew calendar [W] began October 7 3761 BCE Megalomanics (such as Mugabi of Uganda and Kim Il-Sung of North Korea) established their country's calendar to start on their birthdays. The traditional Chinese calendar [W] era begins from the reign of the Yellow Emperor on 2698 BC. But since different Western writers also date the event at 2637 BC or 2697 BC, the Gregorian year 2000 coincides with Chinese years 4637, or 4698 as well as the most common 4697. |
Julian Dates
The Julian Date is a day numbering system " a single consecutive number " useful to reconcile different calendars used among different cultures and historical ages
Originally, the Julian date epoch starts at Noon (12:00), January 1 4713 B.C. --
the beginning of the 7980 year Julian Period ending at 3267 A.D.
The Modified Julian Date reduces the number of digits in
the day value from 1858 November 17 at 0 hours.
Do not confuse this with the Julian Calendar decreed by Julius Caesar (102-44 BC) in 46 BC. This defined the start of each year on the Kalendae (1st of) January 45 BC with months at fixed lengths. (In 222 BC it was changed back to March until 153 BC). To convert from the Julian to a date on the Gregorian calendar,
Thus, to get closer to actual seasons, we need to add one more leap year every 1200 years.
|
Epochs
The point in time when a date calendaring system begins counting is called an epoch.
Microsoft ExcelMicrosoft Excel stores dates as numbers where
Negative values are used to store dates before the epoch. A zero value in a PC's date field would be midnight, December 30, 1899.
|
=TODAY() To avoid possible confusion with Microsoft Excel's differrent epochs, input dates using Excel function: =DATE(2006, 4, 5) or this function: =DATEVALUE("4/5/2006") Cells containing these functions appear as numbers when referenced. |
Microsoft SQLMicrosoft SQL has two data types using two different epochs:
Microsoft SQL does not provide separate variables for date and time because time values are a fraction of the incremental date epoch value. Because time values are approximate numerics, queries should search ranges of datetime values. A date without decimals is assumed to be 0.0.
AND TimeVal - CAST(FLOOR(CAST(TimeVal AS float)) AS datetime) < '10:01'
WHERE DateVal BETWEEN '2002-02-28' AND '2002-02-28 23:59:59.997'
However, MS-Access users need to use special delimiters for dates:
WHERE DateVal >= #2002-02-28# AND DateVal < #2002-02-29# Bryan Syverson suggests that applications that search for portions of a date/time column (such as for a specific year or hour) would have improved performance if a single date/time column is split into two or more separate columns so that they can each be indexed. So, at the time the database is designed, each date/time column should be identified as to whether it will store both dates and times, dates only, or times only. Microsoft SQL does not provide separate variables for date and time. |
UNIX TimeStamps Based on EpochsTimestamps from Unix machines and Java programs are based on the number of seconds since the beginning epoch of January 1, 1970 at midnight 00:00:00 GMT (Greenwich Mean Time)PHP strtotime(), date and mktime at corz and here. In C, the date() function uses time_t and clock_t data types defined with the time.h header file. c-time
Currently, timestamps are either 10 or 13 digits. 13-digit Timestamps (such as 1121453661023) have an additional 3-digit microseconds. There is a finite limit on how far into the future a timestamp can be set because type 'time_t' is stored as a long integer data type. On 32-bit machines, the maximum timestamp value is 2,147,483,647, which translates into 1/19/2038 3:19:07AM GMT. So upgrade to 64-bit machines and operating systems if you need to work on dates beyond 2038, when the next "Y2K" crisis will occur. Delphi-based executable from Carsten Schmidt Network Time Protocol (NTP; RFC2030 for SNTP, Simple NTP) uses a 32-bit count of seconds from 1900-01-01. UNIX time calculations do not include the ISO 8601:2000 Leap Seconds, such as at the end of December 2005. The previous one announced by the International Earth Rotation Service, in France was in 1999, adding a "positive" leap second (represented as 23:59:60Z). This needed to align the SI with the Earth's mean rotation slowing down by about 7 ms / year due to the drag of sea-tides. |
Pop-up in a new window and put this Google Module on your Google homepage This uses built-in Javascript function Date() to create a date from the timestamp such as Fri Jul 15 11:54:21 MST 2005. This is then converted from local to UTC time using the method .toGMTString(). |
In C# from here:
public long GetEpochTime() { DateTime dtCurTime = DateTime.Now; DateTime dtEpochStartTime = Convert.ToDateTime("1/1/1970 8:00:00 AM"); TimeSpan ts = dtCurTime.Subtract(dtEpochStartTime); long epochtime; epochtime = ((((((ts.Days * 24) + ts.Hours) * 60) + ts.Minutes) * 60) + ts.Seconds); return epochtime; }
Date Formatting Tokens (Language Cross Reference)
In the table here, under the C, CGI column are formatting tokens used by the ANSI C strftime function #config and #timefmt Token Codes.
The VBA column holds the format option used in Microsoft Word menu Insert > Field... > Date and Time > SaveDate > Options... > Add to Field To convert VB into Java format: Sun May 25 19:58:02 PDT 2003 Format(Now, "ddd MMM dd hh:mm:ss ??? yyyy") To provide more granular control of date formats than what is generated by VB6's
fncGetDayOrdinal( Now(), string )
Obtaining the Dates for Daylight and Standard Time Changes The Oracle column lists the format mask codes used in the date_format_mask used in this function:
TO_CHAR( COLUMN_NAME,'DD-MON-RRRR HH24:MI:SS' )
The Java column lists the DateFormat codes modifying internationalized functions getDateInstance and getTimeInstance from package java.util.* used by code such as this:
Date today;
dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,
currentLocale);
today = new Date();
The list of literals used to designate pattern strings:
C# .NET has dates of different pre-set sizes:
ToLongDateString() ToShortTimeString() ToLongTimeString() |
VB Date ConversionsTo parse between these two, use Matthew Ferry and Robert Gelb's set of VB date routinesC# .NET Date ConversionTo convert a string into a DateTime object in C# .Netusing System.DateTime; DateTime dateObj = DateTime.Parse("12/30/2005"); Decimal to C Time: char *DecToTime(float fTime, char *szTime){ int nHrs, nMin, nSec; fTime *= 3600; nHrs = (int)fTime / 3600; nMin = (int)(fTime - nHrs * 3600) / 60; nSec = (int)(fTime - nHrs * 3600 - nMin * 60); wsprintf(szTime, "%02d.%02d.%02d Hrs.Min.Sec.", nHrs, nMin, nSec); return szTime; } MS SQL Date ConversionMicrosoft SQL uses date formatting codes such as "yy" for year in queries such as:SELECT DATENAME(yy,DateOfBirth) AS [Year], Extract portions of the MS SQL current dates:
SELECT DATEPART(DayofYear,GETDATE()) AS DayCount
|
Month Text to NumberPL/SQL functions use the National Language Set (NLS) date format:
ADD_MONTHS('15-MAY-2003',3)
Returns a date 3 months from date in the first parameter.
LAST_DAY('15-MAY-2003')
MONTHS_BETWEEN('15-MAY-2002','15-MAY-2003')
|
formattedDate = DateTime.Today.DayOfWeek.ToString(); From Winrunner's CSO2LIB Function Generator category csofunctions2: get_dow( "12/31/1999", text); In Oracle PL/SQL, to return the name of the next day from date x given: NEXT_DAY(x)
|
vbSunday 1 (default) vbMonday 2 vbTuesday 3 vbWednesday 4 vbThursday 5 vbFriday 6 vbSaturday 7 ISO 8601 and UNI 7180-73 standards has the day of week beginning on Monday.
|
public enum TimeOfDay ( Morning = 0; Afternoon = 1; Evening =2; }Then to output the string "Afternoon": TimeOfDay time = TimeOfDay.Afternoon; Console.WriteLine(time.ToString());
|
|
Leap YearsTo astronomers — who view earth as a celestrial body traveling in space — our planet earth takes 365 days plus a little more than a quarter day to make a complete eliptical orbit (perihelion passage) around the sun. To be precise, the mean "anomalistic" or "solar" year (1994-2000) is (depending on the method of calculation):
However, since our watches and time clocks (are supposed to) tick along regularly at 24 hours a day, each year the clocks on our wall would be over 5 hours off to the precise time when the spring season actually starts in the "tropical" year — on the vernal equinox when (in the northern hemisphere) night and day are nearly the same length. At the first official council of the Christian Churches at Nicaea in 325 AD, the vernal equinox was fixed to a particular date (21 March) each year because Easter celebrations are based on the lunar calendar. However, they didn't adjust for celestial mechanics — the "tropical" year being a little slower that the solar year, due to the earth's "precession" (wabbling like a toy top) about its rotational axis (at 0.0003 radians per year), which amount to about a day every 130 years or, to be precise, one part of time in 26,000. The earth's axis of rotation slowly and steadily changes at a rate that (if held at its current value) would take some 21,000 years to complete a circuit (around a perpendicular through its orbital plane) with respect to the perihelion-to-aphelion axis. In 1985 Lasker defined a tropical year with a formula:
where T = (Julian Date - 2451545 ) / 36525 |
The Astronomical Almanac for the Year 2000 by the United States Government Printing Office. Washington, DC: Navy Dept., Naval Observatory, Nautical Almanac Office, p. C1, 2000. "Astronomical Algorithms" 1991 by Jean Meeus Declercq, Georges. Anno Domini: The origins of the Christian era. Turnhout: Brepols, 2000. Richards, E. G. Mapping Time. Oxford: Oxford University Press, 2000 |
Simon Cassidy believes that the time between Vernal Equinoxes is (for 2000) 365.2424 days or 365 days 6 hours 14 minutes "S&H (Stephenson and Houlden's 1986 theory) curve is probably closest to the truth." Anyway, eventually people noticed that "spring" actually started in late autumn weather. So in 1582 AD Pope Gregorius XIII declared that the day after 4 October 1582 should be 15 October 1582, eliminating the 10 days to set the Vernal Equinox again at 21 March. Since it would be difficult for everyone to adjust their watches an odd amount gradually during the year, we occassionally add a day to the last day in Feburary on a leap year . This happen about every four years, since:
To adjust for that "little under" part, years divisible by 400 (e.g., 1700, 1800, 1900, 2100) are NOT leap years. With the help of Jesuit astronomer Christopher Clavius (1537-1612) (using earlier proposals of Pitatus and Lilius), this invention (303 years with 365 days and 97 years with 366 days) is what makes the Gregorian calendar we use today have vernal equinoxes slippage of less than one hour in every 300 years (until circa 4000 AD) by achieving a mean year of
The need for this has been known since the Egyptian calendar, which defined one year with 365.25 mean days consisting of 12 months with a leap year every 4th year — (365*3 + 366)/4. The clever part about the Gregorian calendar is that leap years can be calculated by a year that is divisible by 4. This math was defined by a Scythian monk named Dionysius Exiguus under Pope Gregory XIII in 525 AD. Thus the name "Gregorian calendar". To be more accurate, the Greek (and now Russian) Orthodox Church early this century made a decision to add one leap-day in 2800 A.D (perhaps a divergance from the Roman Catholic Gregorian Calendar). Instead of this, Cassidy proposes the 33 year "Anni Domini" cycle of leap years first proposed by Pope Gregory's Oriental commissioner, the Syrian patriarch Na'amat allah (who in turn cited Omar Khayyam and other Arabic and Persian solar observations). It is calculated with the rule "February will have 29 days whenever the A.D. year-number, reduced modulo 33, is non-zero and divisible by 4." |
Work DaysTo get the number of working days between two dates (not including these dates) with this MS-SQL UDF within SQL SELECT dbo.GetWorkingDays ('11/13/2000', '12/27/2000') CREATE FUNCTION dbo.GetWorkingDays ( @StartDate datetime, @EndDate datetime ) RETURNS INT AS BEGIN DECLARE @WorkDays int, @FirstPart int DECLARE @FirstNum int, @TotalDays int DECLARE @LastNum int, @LastPart int IF (DATEDIFF(day, @StartDate, @EndDate) < 2) BEGIN RETURN ( 0 ) END SELECT @TotalDays = DATEDIFF(day, @StartDate, @EndDate) - 1, @FirstPart = CASE DATENAME(weekday, @StartDate) WHEN 'Sunday' THEN 6 WHEN 'Monday' THEN 5 WHEN 'Tuesday' THEN 4 WHEN 'Wednesday' THEN 3 WHEN 'Thursday' THEN 2 WHEN 'Friday' THEN 1 WHEN 'Saturday' THEN 0 END, @FirstNum = CASE DATENAME(weekday, @StartDate) WHEN 'Sunday' THEN 5 WHEN 'Monday' THEN 4 WHEN 'Tuesday' THEN 3 WHEN 'Wednesday' THEN 2 WHEN 'Thursday' THEN 1 WHEN 'Friday' THEN 0 WHEN 'Saturday' THEN 0 END IF (@TotalDays < @FirstPart) BEGIN SELECT @WorkDays = @TotalDays END ELSE BEGIN SELECT @WorkDays = (@TotalDays - @FirstPart) / 7 SELECT @LastPart = (@TotalDays - @FirstPart) % 7 SELECT @LastNum = CASE WHEN (@LastPart < 7) AND (@LastPart > 0) THEN @LastPart - 1 ELSE 0 END SELECT @WorkDays = @WorkDays * 5 + @FirstNum + @LastNum END RETURN ( @WorkDays ) END GO This function needs holidays observed for the country and organization, which would ideally be provided by a mechanism available to all, such as a resident table or web service call to a company server.
|
Calendar Software
A website should provide several major calendar services on the internet: The Calendar class provides support for date conversions that were previously implemented by the Date class. The Calendar class is an abstract class that can be extended to provide conversions for specific calendar systems. The GregorianCalendar subclass supports the predominant calendar system used by many countries. The Calendar class provides two constructors-a default parameterless constructor that constructs a calendar with the default TimeZone and Locale objects, and a constructor that allows the TimeZone and Locale objects to be specified. It supplies many constants for accessing days of the week, months of the year, hours, minutes, seconds, milliseconds, and other values. The Calendar class provides a number of methods for performing data comparisons, arithmetic, and conversions. The getInstance() method returns a locale-specific calendar that is a GregorianCalendar object, by default. GregorianCalendarThe GregorianCalendar class is a subclass of the Calendar class that supports calendar operations for most of the world. It supports the eras B.C. and A.D. by defining them as class constants. It provides seven constructors that allow GregorianCalendar objects to be created using a combination of different date, time, time zone, and locale values. Its methods override those provided by the Calendar class.
TimeZoneThe TimeZone class is used to encapsulate the notion of a time zone. It allows you to work in the local time zone, as well as time zones that are selected by a time zone ID. The TimeZone class keeps track of daylight savings time.The TimeZone class provides a single, parameterless constructor that creates a TimeZone object corresponding to the local time zone. The TimeZone class does not define any field variables. The access methods of TimeZone allow you to get a list of available time zone IDs, retrieve the local time zone (from the operating system), get the local time zone offset (and adjust it for daylight savings time), and create TimeZone objects for other time zone IDs. SimpleTimeZoneThe SimpleTimeZone class extends TimeZone to provide support for GregorianCalendar objects. It creates SimpleTimeZone objects using the time zone IDs and offsets defined in the TimeZone class. It provides methods for changing the way daylight savings time is calculated.DateAppThe DateApp program illustrates the use of the date-related classes covered in the previous sections. It shows how Date, GregorianCalendar, and TimeZone objects are created and how to use their methods to access date/time information. The DateApp program is presented in Listing 11.10.The program creates a Date object and a GregorianCalendar object using the default Date() and GregorianCalendar() constructors. The Date object is assigned to the today variable, and the GregorianCalendar object is assigned to the cal variable. The cal variable is updated with the current date by invoking its setTime() method with the Date object stored in today. The displayDateInfo() method is then invoked to display date and time information about the cal variable. The clear() method of the Calendar class is invoked to reset the date of the GregorianCalendar object stored in cal. The set() method is used to set its date to New Year's 2000. There are several versions of the set() method, each of which takes a different set of parameters. The version used in DateApp takes the year, month, and date as parameters. Note that the month value ranges from 0 to 12, where the year and date values begin at 1. The displayDateInfo() method is invoked again to display information about the new calendar date. The displayDateInfo() method creates the days, months, and am_pm arrays to define string values corresponding to the days of the week, months of the year, and a.m./p.m. It then prints a line corresponding to date and time values. These values are retrieved using the get() method of the Calendar class and the Calendar constants corresponding to date/time values. The getTimeZone() method of Calendar is invoked to retrieve the local TimeZone object. The getID() method of the TimeZone class is used to retrieve the local time zone ID string. The output of the DateApp program follows. When you run the program, you will obviously get a different date for the first part of the program's processing. The following are the results that were displayed when I ran the program:
| Perpetual Calendar by Toke Norby Chinese Perpetual Lunar Calendar
.vcs (vCalendar)vCalendar (vCal) is a transport and platform-independent electronic calendaring and scheduling exchange MIME type format for Personal Data Interchange (PDI). The vCalendar 1.0 format describes calendar and task information such as the subject of a meeting, the list of invitees, and date and time. Time values in .vcs text files are set to the Greenwich Mean Time (GMT) 24-hour clock (ISO 8601).Originally developed by Versit Consortium, it has, since December 1996, been adopted by the Internet Mail Consortium. More recently, the Internet Engineering Task Force (IETF) C&S Working Group defined the iCalendar standard to add interactivity such as scheduling of meetings. vCalendar is supported by several products, most notably Netscape Calendar, a part of Netscape Communicator Professional 4.0. An appointment in Microsoft Outlook 2000 can be saved as a vCalendar Format (.vcs) file type. Such files will contain PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN vCalendar file icons can be dragged and dropped on the Calendar folder shortcut on the Outlook toolbar. Those who offer vCal files for download include sports events, SDMS, and Course Decision Assistant developed by Digitas at Harvard University. Write a vCalendar file from an Excel spreadsheet. Create a custom VB class and Active-X DLL to share appointments. VCalSax is a Sax parser which changes data in VCalendar format to a specific XML format DOM tree and a XSL Transform to build VCalendar files.
|
A sample .vcs file:
BEGIN:VCALENDAR PRODID:-//The Kennedy Center//NONSGML Web Calendar//EN VERSION:1.0 METHOD:DISPLAY DAYLIGHT:TRUE;-06;20060407T025959;19961027T010000;EST;EDT BEGIN:VEVENT DTSTART:20061226T183000Z DTEND:20061226T203000Z LOCATION:The Kennedy Center DESCRIPTION:An original Kennedy Center production \nMusic and lyrics by ... SUMMARY:Roald Dahl's Willy Wonka PRIORITY:3 CLASS:PUBLIC BEGIN:VALARM TRIGGER:PT60M ACTION:DISPLAY DESCRIPTION:Reminder END:VALARM END:VEVENT END:VCALENDAR
Related Topics:
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you! |