Coldfusion invalid date format [duplicate] - datetime

I'm working with a script that displays the date and time in ISO 8601 format like so: 2012-05-17T17:35:44.000Z.
but I would like it to display in the normal ColdFusion timestamp format when using the #Now()# notation
... so in this format: {ts '2012-05-17 17:35:44'}
How can I do this?

As of CF 10, ISO-8601 is supported directly by parseDateTime.
<cfset string = "1997-07-16T19:20:30+01:00">
<cfset date = parseDateTime(string, "yyyy-MM-dd'T'HH:mm:ssX")>
Runnable Example on TryCF.com

Pretty sure just a parse and then output will give it to you in the format you want :
#parseDateTime(REReplace("2012-05-17T17:35:44.000Z", "(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*", "\1-\2-\3 \4"))#
Edit: Fixed and tested. ;)

I think this is a more complete solution and elegant solution :
<cffunction name="ConvertISOToDateTime" access="private" returntype="date">
<cfargument name="ISODateString" required="yes" type="string" hint="Properly formed ISO-8601 dateTime String">
<cfscript>
// time formats have 2 ways of showing themselves: 1994-11-05T13:15:30Z UTC format OR 1994-11-05T08:15:30-05:00
local.initial_date = parseDateTime(REReplace(ISODateString, "(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*", "\1-\2-\3 \4"));
// If not in UTC format then we need to
if (right(arguments.ISODateString, 1) neq "Z") {
local.timeModifier = "";
//Now we determine if we are adding or deleting the the time modifier.
if (ISODateString contains '+' and listlen(listrest(ISODateString,"+"),":") eq 2){
local.timeModifier = listrest(ISODateString,"+");
local.multiplier = 1; // Add
} else if (listlen(listlast(ISODateString,"-"),":") eq 2) {
local.timeModifier = listlast(ISODateString,"-");
local.multiplier = -1; // Delete
}
if (len(local.timeModifier)){
local.initial_date = dateAdd("h", val(listfirst(local.timeModifier,":"))*local.multiplier,local.initial_date);
local.initial_date = dateAdd("m", val(listlast(local.timeModifier,":"))*local.multiplier,local.initial_date);
}
}
return local.initial_date;
</cfscript>
</cffunction>

Starting with CF2016, Tim Sylvester's answer becomes :
<cfset string = "1997-07-16T19:20:30+01:00">
<cfset date = parseDateTime(string, "yyyy-MM-dd'T'HH:nn:ssX")>
The mm mask has been deprecated and replaced with nn, which was not available with CF10 and CF11.
Warning : the mm mask behaviour also changed with CF2016 (+ 2018 & 2021), and the previous example would output {ts '1998-08-16 18:00:30'}.

That date string is in ISO format, there is a good example of how to convert it to a CF date object here:
...
<cfreturn ARGUMENTS.Date.ReplaceFirst(
"^.*?(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*$",
"$1-$2-$3 $4"
) />

Use createOdbcDate function. It is best to compare in query.
<cfquery name="GetVisits" >
SELECT v.ExecutiveID, eu.firstname, eu.lastname from Visits where
v.visitDate between #CreateODBCDate(DateFrom)#
AND #CreateODBCDate(DateTo)#
</cfquery>

Related

How do I validate a string to be a date in the format dd-M-y, and not be a past date?

I have a date field and the format is "dd-M-y", example 01-Jan-2013. First I want to check the format which must be "dd-M-y" and secondly the date shouldn't be in the past but can be today and onward.
How would I do that? I would like to use regular expressions but I don't know what a suitable one would be.
You should use DateTime.TryParseExact rather than using Regex to validate your DateTime
string testDate = "01-Jan-2013";
DateTime temp;
if (DateTime.TryParseExact(testDate,
"dd-MMM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out temp) &&
(temp > DateTime.Today))
{
//Valid date and greater than today
}
else
{
//Invalid date or less than today
}
I think you should bind the user to fill the date in correct format instead of checking for it...
The Best solution in this case would be MaskEditExtender

xml and condition by dateTime

XML
<CalendarFairs>
<CalendarFair>
<DateStart>2011-04-05T00:00:00</DateStart>
<DateEnd>2011-04-09T00:00:00</DateEnd>
<Title>aaaa</Title>
<IdExecutive>1</IdExecutive>
</CalendarFair>
<CalendarFair>
<DateStart>2011-04-16T00:00:00</DateStart>
<DateEnd>2011-04-19T00:00:00</DateEnd>
<Title>bbb</Title>
<IdExecutive>2</IdExecutive>
</CalendarFair>
<CalendarFairs>
Code
var elements = from element in doc.Descendants("CalendarFair")
where DateTime.Parse (element.Elements ("DateStart").ToString())==DateTime.Now
select new
{
dateStart = element.Element("DateStart").Value,
dateEnd=element.Element("DateEnd").Value,
title=element.Element("Title").Value,
idExcutive = element.Element("IdExecutive").Value ,
};
foreach (var item in elements)//send this error
{}
System.FormatException: The string was not recognized as a valid DateTime. There is a
unknown word starting at index 0.
why error?
Try to change it as follows:
var elements = from element in doc.Descendants("CalendarFair")
let start = element.Element("DateStart").Value
where DateTime.Parse (start)==DateTime.Now.Date
select new
{
dateStart = start,
dateEnd=element.Element("DateEnd").Value,
title=element.Element("Title").Value,
idExcutive = element.Element("IdExecutive").Value ,
};
EDIT: based on the XML you have posted the query above works pretty well. Try to test it with this input:
<CalendarFairs>
<CalendarFair>
<DateStart>2011-04-05T00:00:00</DateStart>
<DateEnd>2011-04-09T00:00:00</DateEnd>
<Title>aaaa</Title>
<IdExecutive>1</IdExecutive>
</CalendarFair>
<CalendarFair>
<DateStart>2011-03-20T00:00:00</DateStart>
<DateEnd>2011-04-19T00:00:00</DateEnd>
<Title>bbb</Title>
<IdExecutive>2</IdExecutive>
</CalendarFair>
</CalendarFairs>
Note that I have inserted today's start date. Actually I think the result was empty just because there weren't entries with actual date.
It sounds like one or more of your input <DateStart> strings is not in a valid DateTime format.
Can you post some sample input XML?
It may be that you need to provide the date format using ParseExact - http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

RFC822 to date format using asp

I try converting RFC822 date format (from rss) into a standard date/time format
using asp3.
Thanks
Nice solution - the minutes don't always go into double figures though - you'll need to pad those out if minutes are just one digit (I think CDate removes that zero)
eg.
dim theminutes
...
tempDate = cdate(tempDate)
if Len(Minute(toReturn))=1 then
theminutes = "0" & Minute(toReturn)
else
theminutes = Minute(toReturn)
end if
RFC822_to_date = day(tempDate )&"-"&month(tempDate )&"-"&year(tempDate )&" "&hour(tempDate )&":"&theminutes&":00"
Take a look at the source code of this Classic ASP RSS reader.
There are some funky functions involving the use of JScript, which look like they will work for you.
It looks like you need the VBScript functions parseDate and newDate, and the two JScript functions.
function RFC822_to_date (orginalDate )
tempDate = trim(right(orginalDate ,(len(orginalDate )-instr(orginalDate,","))))
tempDate = left(tempDate ,(len(tempDate)-5))
tempDate = cdate(tempDate )
RFC822_to_date = day(tempDate )&"-"&month(tempDate )&"-"&year(tempDate )&" "&hour(tempDate )&":"&minute(tempDate )&":00"
end function

SelectedDate not setting properly on the load in Flex

SCinitiationtarget.selectedDate = new
Date(rows1[i]['InitiationTarget']);
I am setting my seletedDate in my DateChooser like this. The format i am getting from the Database is 2009-12-30.
Its displaying in correctly.
I believe the date object doesn't recognize the dash as a valid separator. You'll have to some how reformat your date objects.
For example this works:
var date:Date = new Date("2009/12/30");
myDateChooser.selectedDate = date;
But this doesn't:
var date:Date = new Date("2009-12-30");
myDateChooser.selectedDate = date;
For more information on what date formats are valid, see the documentation here: http://livedocs.adobe.com/flex/3/langref/Date.html#Date%28%29
The first argument of Date constructor is called yearOrTimeValue and as its documentation says it accepts either year or time in UTC milliseconds. For proper Date construction use:
new Date(2009, 12, 30)
I got the solution finally.
var dateStr:String = dateFormatter.format(rows1[i]['InitiationTarget']);
SCinitiationtarget.selectedDate = new Date(dateStr);
<mx:DateFormatter id="dateFormatter" formatString="MMM D, YYYY"/>
With this the problem gets solved.
Why not use the parse method of the Date class?
SCinitiationtarget.selectedDate = Date.parse(rows1[i]['InitiationTarget']);

Javascript Date Validation

How to validate particular format date string using Javascript?
I have one date picker which has the display format like "dddd MMMM dd, yyyy"(displaying like this:"Wednesday February 03, 2010".) So i have to validate this format using javascript. Please help me for implementing this..
If you want to check exactly that format, you could use regular expression:
var re = new RegExp( '^(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)\\s*(January|February|March|April|May|June|July|August|September|November|December)\\s*(\\d\\d),\\s*(\\d{2,4})$' );
var date = 'Wednesday February 03, 2010';
if ( ( match = date.match( re ) ) != null )
{ // valid
alert( match );
}
Or if you just need to know if it is a valid date, what format ever, simply convert it:
var dateSec, dateObj, dateStr = 'Wednesday February 03, 2010';
dateSec = Date.parse( dateStr ); // unix timestamp
if ( dateSec ) // not NaN
dateObj = new Date( dateSec ); // date object
If your application is going to require date manipulation methods, you may want to consider using something like the Datejs library.
If you opt for Datejs, you can use the parseExact() method for the validation. It will return a date object if the date is valid, or null if the date is invalid.
Native JavaScript support for date formatting and validation is somewhat limited.
Take a look at http://www.datejs.com/
You can do stuff like Date.parse('my date string')
Datejs or Dojo can do this. With dojo.date.locale.parse:
var dateAsString = "Wednesday February 03, 2010";
var dateObject = dojo.date.locale.parse(dateAsString, {datePattern: "EEEE MMMM dd, yyyy", selector: "date", locale: "en"});
dateObject will contain the Date object, or null if the string does not match the specified pattern. This can work with a fixed language or any native language.
It doesn't seem right that a date picker would use this as a serialized Date format, though. It should use something easier to parse, like ISO8601 representation.

Resources