I found the ParseDateTime function but it only parses a date/time string according to the English (U.S.) locale conventions.
How to parse the date which is in dd/mm/yyyy format?
Try out this:
<cfset TestdateFrom = ParseDateTime("10/9/2010") />
<cfloop index="i" from="1" to="30" step="1">
<cfset TestdateFrom = DateAdd( "d", 1, TestdateFrom ) />
#TestdateFrom#<br/>
</cfloop>
In CF9 there is a LSParseDateTime function.
I don't know whether this will help me or not.
At last should I use java library for this issue?
Looks like this is working:
<cfset formatter = createObject("java","java.text.SimpleDateFormat")>
<cfset formatter.init("dd/MM/yyyy")>
<cfset newDate = formatter.parse("10/09/2010")>
#newDate#
Any other suggestions?
If your format is consistent, you can also do something like this:
<cfset dy=listGetAt(dateString,1,"/")>
<cfset mo=listGetAt(dateString,2,"/")>
<cfset yr=listGetAt(dateString,3,"/")>
<cfset myDate=createDate(yr,mo,dy)>
But, really, Tim's answer is the best.
I've had success with this technique for dealing with (UK formatted) dates entered as free text:
<cfset SetLocale("English (UK)")>
<cfset valid = true>
<!--- Convert DD-MM-YYYY or DD.MM.YYYY to DD/MM/YYYY --->
<cfset dt = replacelist(dt, ".,-", "/,/")>
<!--- count date elememnts (must be 3 - LSParseDateTime will make potentially incorrect assumptions otherwise) --->
<cfif listlen(dt, "/") neq 3>
<!--- wrong number of elements --->
<cfset valid = false>
<cfelse>
<!--- Correct number of elements so try to interpret as date/time object --->
<cftry>
<cfset dt = LSParseDateTime(dt)>
<cfcatch type="Expression">
<cfset valid = false>
</cfcatch>
</cftry>
</cfif>
If valid is true at the end of this, the string date representation in dt has been converted to a date/time object. The replacelist step allows for the date to be entered as DD.MM.YYYY or DD-MM-YYYY as well as DD/MM/YYYY.
You need to use lsParseDateTime(). This is the locale specific date parser.
You can either pass a locale straight into the second parameter of the function or use setlocale() to set it for the request.
You'll probably find it useful to look at the other LS prefixed functions too.
Related
I have form where user can pick start/end date and time, after from is submitted I want to create createDateTime and use passed arguments. So I tried this way but I got only date stored in my DB. Here is my code:
<cfargument name="DateFrom" type="date" required="yes">
<cfargument name="DateTo" type="date" required="yes">
<cfargument name="TimeFrom" type="string" required="yes">
<cfargument name="TimeTo" type="string" required="yes">
<cfset StartDateTime = createDateTime(year(arguments.DateFrom), month(arguments.DateFrom), day(arguments.DateFrom), hour(arguments.TimeFrom), minute(arguments.TimeFrom), 0)>
<cfset EndDateTime = createDateTime(year(arguments.DateTo), month(arguments.DateTo), day(arguments.DateTo), hour(arguments.TimeTo), minute(arguments.TimeTo), 0)>
<cfquery name="addReservation" datasource="test">
Insert Into tableReserv(PickDateTime,DropDateTime)
Select <cfqueryparam cfsqltype="cf_sql_date" maxlength="12" value="#StartDateTime#">,
<cfqueryparam cfsqltype="cf_sql_date" maxlength="12" value="#EndDateTime#">
Where Not Exists(Select UserID
From aviRequests
Where PickDateTime < <cfqueryparam cfsqltype="cf_sql_timestamp" value="#StartDateTime#">
And DropDateTime > <cfqueryparam cfsqltype="cf_sql_timestamp" value="#EndDateTime#">
)
Select SCOPE_IDENTITY() As RecID;
</cfquery>
For some reason my StartDateTime and EndDateTime do not create date and time together. Here is how my value looks stored in DB:
Start:2016-01-20 00:00:00.000
End:2016-01-21 00:00:00.000
Is my createDateTime properly formatted or something else is wrong in my code?
This is from your question:
<cfset StartDateTime = createDateTime(year(arguments.DateFrom)
, month(arguments.DateFrom)
, day(arguments.DateFrom)
, hour(arguments.TimeFrom)
, minute(arguments.TimeFrom), 0)>
The date part is fine because your date arguments are dates. The time part is not because your time arguments are strings. You have to process that string to get the hours and minutes.
In the comments, you said your time strings resemble 7 AM. You have to process this to get the correct number of hours and minutes. Things you have to consider are:
Are the last two characters AM or PM?
Does the number of hours have 1 or 2 digits?
If it's not at the top of the hour, how are minutes represented?
Having said all that, you are probably not the first person to have this problem. Look at cflib.org to see if there a function you can use.
The Sqlite normal date string format is yyyy-MM-dd. How can I save it as dd/MM/yyyy? Should I keep the yyyy-MM-dd format when working with the database and use dd/MM/yyyy only with the end user interface?
Edit 1:
I want to save my date datas as 'dd/MM/yyyy' in SQLite to be the same as my locale date format. But the date comparison is return wrong result with this format. I changed the date string in DB to 'yyyy-MM-dd' and the date comparison work perfect. Is 'yyyy-MM-dd' is the only string format which SQLite can understand as date?
You can use the strftime() function which accepts as its first parameter a format string:
SELECT strftime('%d/%m/%Y', 'now');
Output:
06/10/2015
The strftime() function gives the ability to convert Y-m-d into d/m/Y or any other format you want. So you can continue to store your date data as is in the database, and you can query it out however you wish.
you can use date formatter code:
NSString *myString = #"2012-11-22 10:19:04";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"dd-MMM-yyyy";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
Having headache day again. Need to make some changes to some things and populate available stuff first. Basically from when something is open till it closes. And add available spots every 15 minutes.
I can get the start date and end date populating - but can't get the inside loop populating the increment times that need to be inserted... Little lost here... Any help is appreciated...
Start Date and End Date come from a cfform - works fine...
Start Date:<br>
<cfinput type="datefield" name="startDate" required="true" message="Start Date Required">
<br><br>
End Date:<br>
<cfinput type="datefield" name="EndDate" required="true" message="End Date Required">
<cfloop index="dtToday" from="#StartDate#" to="#EndDate#">
<cfoutput>
<br>#DateFormat(dttoday)#<br>
<cfloop index="incr" from="#TimeFormat(sadd.topen)#" to="#TimeFormat(sadd.tclose)#" step="#CreateTimeSpan( 0, 0, sadd.increment, 0 )#">
[#TimeFormat(incr)#]
Do Database Insert of Date/Time at increments
</cfloop>
<cfset schdate = #DateAdd('d', 1, '#schdate#')#>
</cfoutput>
</cfloop>
I modified your code so that I could test it quickly. CF Live shows this to work in Railo and CF.
It isn't clear where your trouble is coming from because I didn't make any drastic changes to your code.
<!--- You can get rid of these two form declarations, and the sadd declaration. This was just mimicking your data. --->
<cfset form.startdate = "11/17/95">
<cfset form.enddate = "12/20/95">
<cfset sadd = {topen= "13:00", tclose= "17:00", increment = 15}>
<cfif isDate(form.startdate) and isDate(form.enddate)>
<cfloop index="dtToday" from="#form.StartDate#" to="#form.EndDate#">
<cfoutput>
<br>#DateFormat(dttoday)#<br>
<cfloop index="incr" from="#TimeFormat(sadd.topen)#" to="#TimeFormat(sadd.tclose)#" step="#CreateTimeSpan( 0, 0, sadd.increment, 0 )#">
[#TimeFormat(incr)#] - Do Database Insert of Date/Time at increments<br>
</cfloop>
<!---<cfset schdate = #DateAdd('d', 1, '#schdate#')#>--->
</cfoutput>
</cfloop>
<cfelse>
One or both of the dates entered is invalid.
</cfif>
I changed:
I form scoped startdate and enddate, it shouldn't have any negative impact on your code, it just seemed logical that startdate. While this is proper practice, the only way it could be causing you issues is if you had variables in multiple scopes with the name of startdate and/or enddate. (Safety against that, and security against exploitation of that is exactly why scoping your variables is best practice).
For my sample data, I set start time to 13:00 (1:00 PM) and end time to 17:00 (5:00). You can also use normal format of "1:00 PM" and "5:00 PM".
I commented out schDate because its use wasn't apparent here.
I added a sadd scope to mimic what it seems like your data probably is. In the future, give samples of what your data actually looks like :)
I am using CF8 and MySQL 5.
I have a form with several date fields (one for each day and the form may have 10+ days of data on it) that the user can select different dates for and they all have different var names within a loop.
The default values for these date fields is null in the DB. I can insert a date into a DATE column in MySQL with no issues and have verified that the data is inserted correctly (using cfqueryparam DATE also).
I have the 'value' of the form datefield set to the variable name and can not get the value to show up.
The date updates to the DB fine every time it is entered, but when the form posts back to itself the date fields are blank (other non-date fields work fine and changes show up).
Then when I submit it with the blank date fields the value is set back to null in the DB (empty string in the form) since the form field does not pull the value from the DB.
The field name (ses#i#Date) shows up correctly (ses1Date, ses2Date, etc...) in the form with the right value when I dump it.
<cfloop from="1" to="#form.days#" index="i"> <cfinput type="datefield" name="ses#i#Date" value="#DateFormat(qGetUWHeader["ses#i#Date"],"yyyy-mm-dd")#" /> ....
Thanks for your time and help.
To reference dynamic column names, you can use array notation. But as I mentioned, you must supply a row number.
#queryName["columnName"][rowNumber]#
If you know the query contains one (1) record, only, you could use the query object's "recordCount" property as the row number. Alternatively, you could hard code the row number "1". (Personally, I dislike hard-coding). But any one of these should work.
<!--- pick ONE option --->
<cfloop from="1" to="#form.days#" index="i">
<!--- syntax option 1 --->
<cfinput type="datefield" name="ses#i#Date" value="#DateFormat(qGetUWHeader['ses#i#Date'][qGetUWHeader.recordCount], 'yyyy-mm-dd')#" />
<!--- syntax option 2 --->
<cfinput type="datefield" name="ses#i#Date" value="#DateFormat(qGetUWHeader['ses'& i &'Date'][qGetUWHeader.recordCount],'yyyy-mm-dd')#" />
<!--- syntax option 3 --->
<cfinput type="datefield" name="ses#i#Date" value="#DateFormat(qGetUWHeader['ses#i#Date'][1], 'yyyy-mm-dd')#" />
</cfloop>
If however, you are looping through multiple records in the qGetUWHeader query, you can use the query object's "currentRow" property as the row number. But based on the field naming convention, I am guessing the query only contains one (1) record.
EDIT:
I forgot about the initial nulls. You could apply a simple if condition, and only call DateFormat() if the query value is a valid date.
<cfloop from="1" to="#form.days#" index="i">
<cfset dateValue = qGetUWHeader["ses#i#Date"][qGetUWHeader.recordCount]>
<!--- if this is a valid date, format the value --->
<cfif IsDate(dateValue)>
<cfset dateValue = dateFormat(dateValue, "yyyy-mm-dd")>
</cfif>
<cfinput type="datefield" name="ses#i#Date" value="#dateValue#" /><hr>
</cfloop>
Another option is to format the dates in your SQL. Then you would not need to use CF's DateFormat() function. Just be aware that the new result would be a string, not a datetime object.
SELECT DATE_FORMAT(ses1Date, '%Y-%m-%d') AS ses1Date, ....
Where is "qGetUWHeader" defined?
When a form is posted, all values will be posted to the form scope. Therefore in order to display a value from a postback, you should reference the form scope unless you're copying the value into 'qGetUWHeader'. Even though the correct date from the form is being stored in the form scope, you're displaying the default value from qGetUWHeader on post back, unless you're doing something there I'm not aware of. Then the next time you post your form, the default value overrides the prior entered value.
<!--- In order to reference FORM values, you must CFPARAM them first to define the default value. --->
<cfparam name="form.days" default="10">
<cfloop from="1" to="#form.days#" index="i">
<cfparam name="form['ses#i#Date']" default="">
</cfloop>
<!--- Display the Form --->
<cfform action="#cgi.SCRIPT_NAME#" method="post">
<cfloop from="1" to="#form.days#" index="i">
<cfset thisFieldName = "ses" & i & "Date">
<cfset thisFieldValue = form["ses#i#Date"]>
<cfoutput>#thisFieldName#</cfoutput> <!--- For Debugging --->
<cfinput type="datefield" name="#thisFieldName#" value="#thisFieldValue#" /><br /><br />
</cfloop>
<input type="submit" name="submit" value="submit" />
</cfform>
<!--- Debug --->
<cfdump var="#form#">
I have a string of "DD/MM/YYYY HH:MM:SS" which I need to transform to a date, the problem is the default conversion goes "MM/DD/YYYY HH:MM:SS" unless the day is >12 in which case it switches. I'd like to ensure that my day's go into the day portion of the date/time.
Is there an easy fix to this?
' Parse a date in ISO 8601 "universal combined" format: YYYY-MM-DDTHH:MM:SSZ
' This function ALSO accepts SQL date format: YYYY-MM-DD HH:MM:SS
Function CDateFromUniversal( s )
CDateFromUniversal = CDate(Mid(s, 1, 10) & " " & Mid(s, 12, 8))
End Function
use the format function
this could do it too