Better Time and Number Formats with ColdFusion

July 26, 2019
Host CF Alive podcast, founder CFUnited, CEO TeraTech
Wizard 43 posts
Followers: 14 people
2

Better Time and Number Formats with ColdFusion

Host CF Alive podcast, founder CFUnited, CEO TeraTech
Wizard 43 posts
Followers: 14 people
July 26, 2019

If you’re trying to lose some credibility with your company’s programming, using the wrong date and time formats is a surefire way to do it. Yet, you will be surprised at just how many CF’ers tend to mix these up.

It may sound like a simple mistake, but overlooking something so trivial may lead your clients to wonder what else could go wrong. That’s why it’s important that all CIOs ensure that their development team is using the proper functions, no matter how small.

In this article, you will learn:

  • Why these functions are so important for CIOs
  • The ColdFusion Number Format
  • The ColdFusion Date Format
  • The ColdFusion Time Format
  • The ColdFusion DateTime Format

What makes these functions are so important for CIOs?

These are actually some of the most important functions to get correct-especially if you are doing global business. Take a look at this date.

03/04/05

What do you see? Well, that differs enormously based on where you are from, where you are currently located in the world and how your company does business.

  • In the United States, that could be read as March 4th 2005.
  • In some European countries, this is actually the 3rd of April 2005.
  • In Japan it will be perceived as April 5th 2003.

Therein lies the problem. This can easily be fixed by using the proper ISO (International Organization for Standardization) protocol. But can your developers comfortably provide that standard?

The time and number formats in ColdFusion can be some of the most easily confused functions due to their similarities.

So let’s break down each function and clarify the differences.

1. ColdFusion NumberFormat

The NumberFormat function creates a custom formatted number value. This particular function supports the US standard for numbering. However, if you are looking to get into International numbering systems, you can use a separate function — the LSNumberFormat .

The NumberFormat can also be used to return special formats depending on one of two conditions.

  1. If no mask is specified, an integer with a thousands counter is returned.
  2. If the parameter value is an empty string, a zero digit can be returned.

There are two major parameters to the NumberFormat function: The number and the mask. The number is just the number to be used. The mask, on the other hand, defines special characteristics about the number.

This table represents the Mask Characters that may be used with this function.

NumberFormat Masks and Meanings

The NumberFormat uses Java standard locale formatting rules on all platforms. The position of symbols in format masks determines where the codes take effect.

2. ColdFusion DateFormat

The DateFormat function provides a custom formatted date value. These dates are in US standard formats. Those looking to use international standards can use the LSDateFormat . This function is best-served formatting output instead of input. Use the CreateDate function for input.

This function will return a standard format of unless otherwise specified in the mask of the function.

The DateFormat also has two parameters, the date and the mask.

Remember, date and time values from query results may vary significantly in format unless you use functions to format them to a standard. Use the LSDate and LSTime functions in coordination with this function to format resultset values.

4. ColdFusion TimeFormat

TimeFormat is the basic time-formatting function offered by ColdFusion. It is based on US Standard conventions. Those looking to express International time conventions should use the LSTimeFormat function.

As with the previous functions, the TimeFormat function has two parameters. The time and the masks.

5. TimeFormat Masks and Meanings

When using this function, it is imperative that you enclose the string within quotation marks. Failing to do so will interpret the function not as a string but as a Date/Time Object.

This function formats both date and time values collectively as per Date and Time Standards. The parameters are the date, the masks, and the timezone.

This uses the same masks as both the Date and Time functions.

Here is an example from Adobe on how the DateTimeFormat function should be written and executed:

<cfscript> Date1 = "{ts '2018-11-05 12:13:50'}"; DateTimeFormat= DateTimeFormat(Date1,"m") DateTimeFormat1= DateTimeFormat(Date1,"mm") DateTimeFormat2= DateTimeFormat(Date1,"mmm") DateTimeFormat3= DateTimeFormat(Date1,"mmmm") DateTimeFormat4= DateTimeFormat(Date1,"M") writeOutput("Month as digits; no leading zero for single-digit months: " & DateTimeFormat & "") writeOutput("Month as digits; leading zero for single-digit months: " & DateTimeFormat1 & "") writeOutput(" Month as a three-letter abbreviation.: " & DateTimeFormat2 & "") writeOutput(" Month as a three-letter abbreviation.: " & DateTimeFormat3 & "") writeOutput(" Month in year: " & DateTimeFormat4 & "") </cfscript>

As you can see, these formats and functions are not particularly difficult. However, these are often mixed up due to their striking similarities. And well, that’s not good.

It would be terrible if you missed a key event due to a misformatted date or time. That’s a rather embarrassing mistake that devalues the credibility of your organization. Make sure that both you and your team are well-versed in these functions.

Have you ever heard of a date/time mishap leading to disaster? What do you do in your workplace to ensure these mistakes don’t get made?

Let us know in the comments below. We’re always down for a good story and some learned life experience. Also,if you like what you see, you can always follow us on Twitter @CFTeraTech and make sure you don’t miss an update.

And to continue learning how to make your ColdFusion apps more modern and alive, I encourage you to download our free ColdFusion Alive Best Practices Checklist.

Because… perhaps you are responsible for a mission-critical or revenue-generating CF application that you don’t trust 100%, where implementing new features is a painful ad-hoc process with slow turnaround even for simple requests.

What if you have no contingency plan for a sudden developer departure or a server outage? Perhaps every time a new freelancer works on your site, something breaks. Or your application availability, security, and reliability are poor.

And if you are depending on ColdFusion for your job, then you can’t afford to let your CF development methods die on the vine.

You’re making a high-stakes bet that everything is going to be OK using the same old app creation ways in that one language — forever.

All it would take is for your fellow CF developer to quit or for your CIO to decide to leave the (falsely) perceived sinking ship of CFML and you could lose everything-your project, your hard-won CF skills, and possibly even your job.

Luckily, there are a number of simple, logical steps you can take now to protect yourself from these obvious risks.

No Brainer ColdFusion Best Practices to Ensure You Thrive No Matter What Happens Next

ColdFusion Alive Best Practices Checklist

Modern ColdFusion development best practices that reduce stress, inefficiency, project lifecycle costs while simultaneously increasing project velocity and innovation.

Easily create a consistent server architecture across development, testing, and production

A modern test environment to prevent bugs from spreading

√ Automated continuous integration tools that work well with CF

A portable development environment baked into your codebase… for free!

Learn about these and many more strategies in our free ColdFusion Alive Best Practices Checklist.


Originally published at https://teratech.com on July 26, 2019.

Comments (2)
2019-07-26 19:25:32
2019-07-26 19:25:32

Michaela, thanks for the tutorial, but something has gone quite wrong in the translation of this page from your site to here. There is much info missing (tables of info, and results not shown), probably requiring some tweaking on your part of the html here. Hope you will do it, but until then folks should look to the link at the bottom for how things look on the original page at teratech.

Like
(1)
>
Charlie Arehart
's comment
2019-08-16 18:35:29
2019-08-16 18:35:29
>
Charlie Arehart
's comment

Charlie

thanks. Yes, I will look into it.

Like
Add your comment