Using DATE functions in Coldfusion

There are some useful date functions in Cold Fusion which makes life easy for developers when working date and time date. In this article, we can take a look at few date functions and examples for those functions. Now() This function will return the current date and time from the cold fusion server. Examaple, Code: #now()# Dateformat() This is the function to format a date in any format given in the second argument. It takes two arguments first argument date and second one is mask Example, Code: #dateformat(now,"dd-mm-yyyy")# Following are the possible mask you can use with dateformat(). Code: Day of the week : d , dd, ddd , dddd Month : m, mm, mmm, mmmm Year : y, yy ,yyyy Timeformat() This function format a time based on mask passed to the function. Example, Code: #Timeformat(now,"hh:mm:ss")# Following are the possible mask you can use with dateformat(). Code: Hours : h (12-hour clock) , hh (12-hour clock), H, HH Minutes: m, mm,s,ss Time Marker (AM OR PM): t, tt How to check a date is past date or future date using CFM? For checking a date is a past date or not, datecompare() can be used for this. Example, Code: Date is past date For checking a date is a future date or not, use datecompare() as follows Code: Date is a future date Creating a new date in CFM For creating a new date use createdate() function. Year , month and day can be passed to this function. Example, Code: Createdatetime() This function creates a valid date time object. Format is CreateDateTime(year, month, day, hour, minute, second). Example, Code: Createtime() This function creates a valid time object. Format is CreateTime(hour, minute, second). Example, Code: CreateODBCDate() This is the very useful function when you insert a date to database query from cold fusion. This function makes a valid ODBC date from a date string passed. CreateODBCDateTime() can be used to create a valid ODBC date time object. Example, Code: #CreateODBCDate(now())# DateDiff() This function can be used for finding difference between two dates. Format is DateDiff(datepart, date1, date2) . Example, Code: #DateDiff("d",now(),"01-01-2008")# Date part can be one of the following Code: yyyy - Year q - Quarter m - Month y - Day of year d - Day w - Weekday ww - Week h - Hour n - Minute s - Second DateAdd() This funcation can be used for adding a time interval to given date. Format is DateAdd(datepart, number, date). Example, Code: #DateAdd("d",10,now())# Date part can be same as Datediff(). Following are the other useful CFM date functions: DayOfWeek, DayOfWeekAsString, DayOfYear, DaysInMonth, DaysInYear, FirstDayOfMonth ,Week and Year. Please post your comments about this article. Thanks

Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net