Date formats

by Gisle Hannemyr

This chapter shows how to create a custom date format.

Table of contents

Introduction

Before you start, you should check the general settings for date and time and make sure they are correct.

See alsoYou may also want to read the community documentation for Date/Calendar. It contains a presentation of the date field types, the date locale, the date API, date form elements and guides to common date and calendar applications.

Creating a custom date/time format

Sometimes, we need other date formats than those that already exists. The tutorial below shows how to create a custom format that works just like the predefined formats.

To begin the process, navigate to Administration » Configuration » Regional and language » Date and time. Take a look at the three default date formats provided. You may want to change these to formats that are more familiar to your target audience than the defaults that are set.

However, in this tutorial we are going to create a fourth format that just displays the time. This is because we know that we are going to need a compact time-only format in our calendar view.

To do this, click on the Formats tab and then on the Add format link. The syntax for the is these formats are same as used by PHP.

For instance, to add a 24 hour hour:minute format, use “H:i” as the format string.

date02.png

After defining the format string, click on Add format.

Next, we need to assign the newly defined format to a format with a specific name. To do that return to list of date and time formats by clicking on the Types tab. To add the newly created format to the list, click on the link Add date type.

In the fields under Date type, enter a descriptive name. A machine name will be created automatically. Select the date format to associate with this name.

date03.png

After naming the format, click Add Date type.

The new format name will now appear in the list of formats.

date03.png

When done defining a new data format, do not forget to save your configuration.

Final word

[TBA]


Last update: 2016-09-20.