Vous êtes sur la page 1sur 4

IT Services

Microsoft Excel 2010 Dates and Times (Level 3)

Contents
Introduction ..............................................................................................................1 Date/Time Formats .................................................................................................1 Date/Time Functions ..............................................................................................2 Exercise ........................................................................................................................3 System Settings ........................................................................................................4

Introduction
Though Microsoft Excel displays dates and times as you would expect, in fact it stores them as numbers (unless they are prior to 1900). Dates are stored as the number of days since the beginning of the last century (ie 1-Jan-1900 is day 1). Some systems (eg Apple Macs) use 1-Jan-1904 as day 1 and Excel has an option which can be set to cope with this (in the Advanced section of Excel Options). Note that dates before 1900 are stored as text and cannot be used in calculations. Times are stored as parts of a day - ie 12 noon is stored as 1/2 or 0.5. Both the date and time can therefore be stored as a single number, the fractional part forming the time. Indeed dates and times both have an implicit other part - eg dates are held as being at midnight (0:00:00am). Any calculation which results in a time fraction greater than 1 simply adds a day to the result.

Date/Time Formats
When entering a date into a cell, Excel will accept various different formats (including year/month/day as well as day/month/year). Months can be entered as a number or text - the name in full or in an abbreviated (threeletter) form. Years abbreviated to 2 figures are taken to be between 1930 and 2029 - ie 31/12/20 is 31-Dec2020. This can be amended if necessary (see later). If the year is omitted, the current year is assumed. A slash or hyphen (minus sign) can be used to separate the day, month and year. Spaces can be used if the month appears as text. Excel even accepts 31Dec but note that Dec31 is December 1931. If Excel recognises data as a date it will appear on the right of the cell - if it appears on the left, Excel hasn't recognised it and it is stored as text. Data is displayed in a standard date format (with slashes or with hyphens if the month is entered as text). These defaults are those used by the system and can be altered (see later). Times are entered into a cell by using a colon to separate the hour, minute and second. They also appear right-justified if recognised as valid. Because the time represents the fraction of the day, the hour must always

be included. Times can be entered with an am/pm suffix if desired (a space must precede am/pm). Both a date and time (separated by a space) can be entered into a cell. Once a date and/or time has been entered into a cell, various format styles can be applied to the data. To apply a format: 1. Enter a date into an empty cell (as dd/mm/yy) and press <Ctrl Enter> to stay in the cell Tip: To enter todays date, simply press <Ctrl ;>; to enter the current time, press <Ctrl :>. 2. Click on the [Number Format] button in the Number group on the Home tab and choose Long Date the date appears showing the month in full 3. Repeat step 2 but choose More Number Formats 4. Select the Type: desired then press <Enter> for [OK] to apply the chosen format Formats can also be customised if you require a format not built in to the system. Here's an example: 5. Move to an empty cell and enter a time (eg 9:00) - press <Ctrl Enter> to stay in the cell 6. Click on the [Number Format] button and choose More Number Formats 7. The Category: is already set to Custom, scroll down and choose hh hours mm minutes 8. Press <Enter> for [OK] then repeat step 6 9. Click in the Type: box and amend it to read h "hours" m "minutes" s.00 "seconds" 10. Press <Enter> for [OK] to apply the custom format The recognised date format codes include: d (days as 1-31), dd (days as 01-31), ddd (Mon-Sun), dddd (Monday-Sunday), m (months as 1-12), mm (months as 01-12), mmm (Jan-Dec), mmmm (JanuaryDecember), mmmmm (J, F ... D), y or yy (years as 00-99), yyyy (years as 2000). Time format codes include: h and hh (hours as 0-23 or 00-23), m and mm (minutes as 0-59 or 00-59), s and ss (seconds as 0-59 or 00-59), ss.00 (fractions of a second), [ss] (elapsed time in seconds), h:mm:ss AM/PM (12-hour clock with AM or PM). Note that m or mm on its own gives months not minutes! So, your custom format shows hours, minutes and seconds without leading zeroes (for values less than 10) with the time shown to the nearest hundredth of a second!

Date/Time Functions
Because dates (and times) are stored as numbers, they can be used in calculations - for example, it's easy to work out the number of days between two dates or calculate an employee's wages given an hourly wage rate. If you are dealing with historical data (pre-1900) and want to carry out any calculations, then you either have to store it as post-1900 or you have to use functions to calculate an equivalent date which is post-1900. NOW() TODAY() gives today's date and time (eg 21/07/2008 09:30) - the two brackets are required gives today's date in date format (eg 21/07/2008)

1. Type =Now() into any empty cell 2. Type =Today() into another empty cell 3. Type =Now() into the cell with your custom format only the time is shown 4. Hold down <F9> and watch the time being updated Note that NOW() and TODAY() are refreshed whenever a change is made to the worksheet. An option can be set to recalculate only when required (ie when key <F9> is pressed) turn on Manual on the [Calculation Options] button in the Calculation group on the Formulas tab.

In the following functions, the value for date/time can be supplied either as text (enclosed in quotes) or a number: DAY(date) MONTH(date) YEAR(date) HOUR(time) MINUTE(time) SECOND(time) isolates the day of the month from a date - eg DAY(NOW()) isolates the month from a date - eg MONTH(NOW()) isolates the year from a date - eg YEAR(NOW()) isolates the hour from a time - eg HOUR(NOW()) isolates the minutes from a time - eg MINUTE(NOW()) isolates the seconds from a time - eg SECOND(NOW())

5. Try out some of the above functions in empty cells to see what they do Note also that the TEXT(date/time,format) function can be used to generate results similar to the above but in a text format (which cannot be used in calculations). For example, TEXT(NOW(),"dddd") might give Thursday while TEXT(NOW(),"h:mm") might produce 9:45. Further recognised format codes were listed at the end of the previous section. Less common functions include: DATE(year,month,day) TIME(hour,minute,second) DATEVALUE("date") translates separate numbers into a number date format translates separate numbers into a time date format translates a date stored as text into a number (which can then be displayed as a date by applying a format) as DATEVALUE but for a time Returns the number of working days between two dates; holidays is an optional parameter containing any dates not worked gives the number of the day of the week (Sun=1 if type not set) gives the week of the year (starts on Sun if type not set) gives a date number of working days after the set date - used for invoices to be paid within a certain number of days gives the date for the number of months before/after the start date

TIMEVALUE("time") NETWORKDAYS(start_date, end_date, holidays) WEEKDAY(date,type) WEEKNUM(date,type) WORKDAY(date,number)

EDATE(start_date, months)

Exercise
Tip: When using dates and times, it's all too easy to accidentally set up a special format on a cell which then affects the display of any data (or result of a formula) entered into that cell. To clear a format on a particular cell, click on the [Clear] button in the Editing group on the right of the Home tab and select Clear Formats. 3

1. On a new worksheet, type your date of birth into cell A1. 2. (In cell A2) Use the TEXT function to tell you on which day of the week you were born. 3. (In cell A3) Use a function or key combination to display today's date. 4. (In cell A4) Using the results from (1) and (3), calculate how many days you have lived (a simple subtraction). Note: you will need to clear the cell format or change it from a date to an ordinary number. 5. Format the result in cell A4 to read xxxxx days 6. (In cell A6) Use a function and format to display the current time (eg 11:32:15). 7. [Copy] the result from cell A6 and use [Paste] then Paste Special to paste first the value and then the format into cell A7. 8. (In cell A8) Using the data from cells A6 and A7 calculate how much time has elapsed between the original calculation (held in A7) and the completion of this particular task. 9. Fix the values displayed in cells A6 and A8 by changing recalculation from automatic to manual. 10. Press a key to refresh (recalculate) the values in A6 and A8. Now reset calculation to automatic. 11. Apply a format to the value held in cell A8 to show the elapsed time in seconds. Try to modify this format to show seconds to two decimal places If you want, you can check your answers by clicking on the link provided here. Close the worksheet by clicking on the [Close Window] button - save the results if you want to. 12. [Close] your file theres no need to save it (unless you want to)

System Settings
The default way dates and times are displayed by Excel (and in the system generally) can be changed at the Windows level, if you have the privileges (you don't have them on the public PCs). You can also amend the default settings for 2-digit year interpretation (from 1930-2029). If you want to alter these settings you need to go to the Control Panel. DO NOT CARRY OUT THE FOLLOWING INSTRUCTIONS UNLESS YOU NEED TO: 1. From the Window's Start button choose Control Panel 2. Click on the Clock, Language, and Region icon 3. Under Date and Time, click on Set the time and date 4. Next click on [Change date and time] then on Change calendar settings 5. On the Time tab in the Customize Format window you can set up your own Time formats 6. On the Date tab you can set up your own Date formats. You also have a When a two-digit year is entered, interpret as a year between: box 7. Theres no need to make any changes keep clicking on [Cancel] to close the dialog boxes

Trademark owned by Microsoft Corporation. Screen shot(s) reprinted by permission from Microsoft Corporation. Copyright 2011: The University of Reading Last Revised: November 2011 4

Vous aimerez peut-être aussi