Why does ColdFusion Format Dates Differently using the same function - date

Consider the following code:
<cfset lateDate = createDate(2014,12,8) />
<cfset currentdate = createDate(2015,4,15) />
<cfdump var="#lateDate#" />
<cfdump var="#currentdate#" />
<cfdump var="#dateCompare(currentdate,lateDate)#" />
Output on my machine is:
{ts '2014-12-08 00:00:00'} {ts '2015-04-15 00:00:00'} 1
Question 1: Why is the month switched on the two dates. (month/day). The spec says create date is yyyy,mm,dd and yet either CF switched them or it's displaying them switched.
It's doing the dateCompare correctly so what is going on. Have I been staring at this too long?

I think you have been looking at it too long. Both of those are valid dates and the month is not being switched. One of your dates is April 15, 2015 and the other is December 8, 2014. I assume you were thinking August 12, 2014 for that date. Then your code should be createDate(2014,8,12). Right?
The dateCompare function only tells you if the first date is earlier or later than the second date. For both of your dates, the correct one and your assumed one, the first date is later than the second.
I think the code is working correctly. As in:
createDate(2014,8,12) <!--- August 12, 2014 --->
createDate(2014,12,8) <!--- December 8, 2014 --->
createDate(2015,4,15) <!--- April 15, 2015 --->
dateCompare "April 15, 2015" "December 8, 2014" returns 1
dateCompare "April 15, 2015" "August 12, 2014" returns 1

Related

Text DateStyle with short month name in SwiftUI

The following outputs the complete month name, e.g. July 11, 2022
Text("\(Date().now, style: .date)") // output: July 11, 2022
Is there a way to make it show just the short month name, e.g. Jul 11, 2022 or 06/11/22?
Show current date without time in a short format.
//Jul 11, 2022
Text(Date().formatted(date: .abbreviated, time: .omitted))
//7/11/2022
Text(Date().formatted(date: .numeric, time: .omitted))
SwiftUI 3+
We have now explicit initializer for that, like
Text(Date.now, format: Date.FormatStyle(date: .abbreviated, time: .omitted))

Date format does not work in R

This is my date column which is on my dateframe:
data <- data.frame(year_month = c("2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01", "2015-05-01", "2015-06-01"))
class(data$year_month)
[1] "factor"
I tried:
data$year_month1<-as.Date(as.factor(data$year_month), format="%b %Y")
I want:
" Jan 2015", "Feb 2015", "Mar 2015", "Apr 2015", "May 2015", "Jun 2105"
What happens:
My column on data$year_month1 comes Empty.
I tried quite a few solutions that I found here but the format does not change at all or it comes empty.
Help, please>
format(as.Date(data$year_month), format="%b %Y")
#first convert to Date, without as.factor (why did you do that?)
#then convert it to character as you want it, with format()

Dateparse MON in Tableau

I have a data source that returns a date as a string in the form of 'MON YYYY' (APR 2014, MAY 2014, etc.).
I tried making a calculated field off of this information with the following formula:
DATEPARSE('MMM YYYY', [Field1])
This is a sample set of the data I'm getting (I added the pipe as a divider):
Field1 || Calculated Field
APR 2014 || 12/22/2013
APR 2015 || 12/28/2014
APR 2016 || 12/27/2015
AUG 2014 || 12/22/2013
AUG 2015 || 12/28/2014
AUG 2016 || 12/27/2015
I've also tried to add a day field, but that results in the same incorrect data as above:
DATE(DATEPARSE('dd MMM YYYY','01 ' +[Field1]))
Is there something I'm perhaps misunderstanding about the dateparse function?
It turns out that YYYY means something totally different than yyyy. The capitalized MMMwas necessary for the MON type description. This worked for me:
DATE(DATEPARSE('MMM yyyy',[Field1]))
If you date the date off you'll get the hour, minute, second fields as well.
Dateparse converted it from a string [Field1] into a Date type using the aforementioned format of three digit month, a space, and a four digit year (e.g. AUG 2014 -> 8/2/2014).

Crystal Reports show current month as a 2 digit field

I wonder if anyone could help on this please? I need to show the current month as a 2 digit field. IE
January as 01
February as 02
March as 03
etc until
October as 10
November as 11
December as 12
The formula I am using is: ToText ("0"& Month(CurrentDate))
but shows January as 01.00
ie need to remove the decimal point and the decimal places
Many thanks, Rob
Try this:
ToText( CurrentDate, "MM")
The ToText function will automatically convert the date you are supplying to whatever format you want. You don't need to use the Month function. Per the documentation, you just supply the date and the output format. For the month, you use "MM".
ToText(CurrentDate, "MM")
According to the documention, these are the valid strings you can use
Pattern Result
d Numeric day of month without leading zero (1, 7, 31)
dd Numeric day of month with leading zero (01, 07, 31)
ddd Three day abbreviation of day of week (Mon, Sat)
dddd Full name of day of week (Monday, Saturday)
M Numeric month without leading zero (1, 7, 12)
MM Numeric month with leading zero (01, 07, 12)
MMM Three letter abbreviation of month (Jan, Feb, Mar)
MMMM Full name of month (January, February, March)
yy Last two digits of year (11, 14, 22)
yyyy Full four digits of year (2011, 2014, 2022)
To add to the above, if you need to return something like Mar-17 then:
totext({Command.DocDate},"MMM") + '-' + totext({Command.DocDate},"yy")

Modifying date in ColdFusion

How can I adjust a date in coldfusion to get the next day at 1AM?
the date is taken from a database, and stored as a string. I'm thinking the way to do it is through CreateDateTime and filling it with the time and date using year,month,day + 1 etc.
I'm just worried that it won't work when the next day falls on the next month
Using DateAdd() you can always be sure that it will take the context of the current date into account. So if it is August 31 and you add one day it will correctly make the date Sept 1st. It will also properly switch the year if you did the same on Dec 31st.
<cfset nextDate = dateAdd("d", 1, now()) />
<cfset nextDateWithTime = createDateTime(year(nextDate), month(nextDate), day(nextDate), 1, 0, 0) />
<cfoutput>#nextDateWithTime#</cfoutput>
Assuming the date is something which CF recognizes as a date, and contains date only, without time, you could do something like:
<cfscript>
function tomorrowOneAM(date) {
var resultValue = DateAdd("d",1,date);
resultValue = DateAdd("h",1,resultValue);
return resultValue;
}
</cfscript>