ColdFusion: Error Passing Date that doesn't have day of week? - datetime

I am trying to parse two date strings into date objects. The code works for one string, but throws an "invalid date time" error for the other. The only difference is that it's got a "Sat, " at the beginning! Please tell me why this is happening and how I can solve it!
<cfset datetimetest1 = "23 Nov 2013 00:53:12 +0000">
<!--- ^ This throws an error (when you try to pass it). Error says 'invalid date time' --->
<cfset datetimetest2 = "Sat, 23 Nov 2013 00:53:12 +0000">
<!--- ^ This works when it is parsed --->
<cfoutput>
#parsedatetime(datetimetest1)# #parsedatetime(datetimetest2)#
</cfoutput>

I identified RSS dates that are being used from the following sources and tested them against isDate(), DateFormat() and LSDateFormat(). ColdFusion 10,286680 was only able to parse 65% of the dates (38 out of 58).
http://rssdateformats.tumblr.com/
https://github.com/mjibson/goread/blob/0387db10bd9fd9ccd90d557fa30b6e494efa577a/goapp/utils.go#L129
Here's the test script that I wrote:
https://gist.github.com/JamoCA/7617349
I've been looking for a Java date parser library and recently found Natty and StringToTime, but haven't used either yet. (Neither resource provides a downloadable JAR file.):
http://natty.joestelmach.com/
https://github.com/collegeman/stringtotime

Update:
As of CF10+, you can use a custom format string to instruct the function on how to parse and convert the input into a datetime object:
// Custom format string
dateObject = parseDateTime("23 Nov 2013 00:53:12 +0000"
, "dd MMM yyyy HH:mm:ss zzz");
This is one area in which CF's flexibility and ease of use can be a disadvantage IMO. Unfortunately parseDateTime does not allow you specify the format of the input string, so CF has to do a lot of guessing to "automagically" convert your string into a date object.
One of the tools CF employs is java's SimpleDateFormat class, which utilizes patterns to parse or convert strings into Dates. My understanding is CF maintains a listing of standard date/time patterns (according to U.S. date conventions). Your first string must not match any of those patterns. Hence the error.
If your date strings are always in UTC, you could simply use list functions to omit the timezone offset ie +0000, then parse the string as usual:
<cfscript>
origString = "23 Nov 2013 00:53:12 +0000";
dateString = listFirst(origString, "+");
WriteDump(parseDateTime(dateString));
</cfscript>
Or you could DIY using SimpleDateFormat and the appropriate pattern dd MMM yyyy HH:mm:ss Z (see Date and Time Patterns). Just note the returned dates are in local time, so you may need to use DateConvert if you want UTC times:
// get formatter based on default locale
formatter = createObject("java", "java.text.SimpleDateFormat").init();
// set up pattern for input date string
formatter.applyPattern("dd MMM yyyy HH:mm:ss Z");
// parse it into a date object
dateObject = formatter.parse("23 Nov 2013 00:53:12 +0000");
// display result in local and UTC time
WriteDump("local="& dateObject);
WriteDump("utc="& DateConvert("local2UTC", dateObject));

Related

Is there any option to read the total project execution time through groovy

Currently I like to calculate the total time taken for my soap ui automation project using groovy. I tried the following approach but it doesn't work:
Date startTime= new Date()
Date EndTime= new Date()
But i unable to compare the dates since it is taking the data types as string "Sat May 18 23:54:29 IST 2019" and I am unable to find the difference.
In groovy you can use the TimeCategory utility to subtract your dates, and get a TimeDuration object representing the difference. From this object you can inspect all sort of structured time/duration information.
Also, if you have a date in a String representation you can parse it into a Date using Date.parse passing as a parameter the format of the string and the string representation itself.
The following is a working demo of all this:
import groovy.time.*
def startTimeString = "Sat May 18 00:00:00 IST 2019"
def startTime = Date.parse("E MMM dd H:m:s z yyyy", startTimeString)
def endTime = new Date()
use (TimeCategory) {
TimeDuration duration = endTime - startTime
println "[${startTimeString}] was [${duration}] ago"
}
Complete code on GitHub
Hope this helps.

Displaying local time from ISO 8601 string with Momentjs

I want to display the local time from an ISO 8601 string using momentjs.
There is a discrepancy of minutes when I convert an ISO string using different date formats. If I use 'MM/DD/YYYY HH:mm', the minutes is correctly displayed. If I use 'ddd, MMM Do HH:MMa', 11 minutes is added (in my case).
My sample js (babel) code:
let today = moment('11/09/2016 00:00', 'MM/DD/YYYY HH:mm').toISOString();
//today = 2016-11-09T08:00:00.000Z
let formatted = moment(today, moment.ISO_8601).format('MM/DD/YYYY HH:mm');
//formatted = 11/09/2016 00:00
let formatted2 = moment(today, moment.ISO_8601).format('ddd, MMM Do HH:MMa');
//formatted2 = Wed, Nov 9th 00:11am
I would prefer using the second format. Can someone explain why there is a discrepancy?
Please see this fiddle: https://jsfiddle.net/anudhagat/8fgtjbc7/3/
I caught my silly mistake. I have capitalized the minutes in the second format, using MM makes it display months instead of minutes.

Need to format time zone offset using moment js

I have a datepicker on my page, when I select any date it produced a result something like
Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)
And I need to format it: YYYY-MM-DDTHH:mm:ss Z
So, for this I use moment syntax
var date='Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)';
moment(date).format('YYYY-MM-DDTHH:mm:ss Z');
which produces an output
2014-09-07T00:00:00 +05:00
That's good, but my api expect standard timezone offset 'Z' instead of parsing into local current time zone (i.e +5:00) in my case.
So, I want to produce this
2014-09-07T00:00:00Z
How is it possible?
Use moment.utc() to display time in UTC time instead of local time:
var dateValue = moment(date).utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';
or moment().toISOString() to display a string in ISO format (format: YYYY-MM-DDTHH:mm:ss.sssZ, the timezone is always UTC):
var dateValue = moment(date).toISOString();
JSFiddle
Rather than using a hard-coded format string and then concatenating the 'Z' after it, it would be better to just use the toJSON() function which has been around since moment js v2.0. Like so:
moment('Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)').toJSON();
Here's the complete JSFiddle.

How To Parse Date From any datetime format to dd/mm/yyyy?

i want to parse any datetime format to dd/MM/yyyy format.
here is my code
// dates i am providing are
// Sat, 01 Oct 2011 17:30:00 +0400
// and
// Sat, 01 October 2011 12:21:23 EST
Datetime dt = Convert.toDateTime(pubDate);
which is giving me following exception
The string was not recognized as a valid DateTime. There is an unknown word starting at index 32
any one guide me how can i parse any dateformat to a single one?
any help would be appreciated.
DateTime doesn't store dates in a "format" - it uses an internal representation. You need to parse a passed in string in order to get the correct value for the DateTime and when you want to display it you can then format it to whatever display.
Your best bet is to use TryParseExact supplying it with the exact format string. You need to use the custom Date and Time format strings with it.
Use the overload that takes a string[] of format strings - one for each date format.
In regards to the EST portion - the framework doesn't have support for named timezones. You may want to write a wrapper that converts named timezones to their equivalent but parseable form.
Untested (based on MSDN example):
string[] formats= {"ddd, dd/MMM/yyyy hh:mm:ss K",
"ddd, dd MMMM yyyy hh:mm:ss EST"};
DateTime dateValue;
foreach (string dateString in dateStrings)
{
if (DateTime.TryParseExact(dateString, formats,
new CultureInfo("en-US"),
DateTimeStyles.None,
out dateValue))
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
else
Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
}
Remove the 'EST' from the string and it should work.

Joda Time formats time at 24:xx:xx UTC should be 0:xx:xx UTC

I'm converting from a local time zone to UTC so when we convert
2010-01-03T11:15:58.840+11:00 => Sun, 03 Jan 2010 24:15:58 UTC
This is technically correct but I'm having problems with the 24 hour formatting as it does. I have some BlackBerry J2ME code which is having problems parsing this date-time String using HttpDateParser.
new Long(HttpDateParser.parse("Sun, 03 Jan 2010 24:15:58 UTC")
Shouldn't this really be "Sun, 03 Jan 2010 0:15:58 UTC"? If I pass in this new date String it will parse just fine. I'd rather not do a nasty "search and replace", but fix the problem at the server.
Question: Is it possible to stop Joda from displaying times as "24:xx:xx" and instead format as "0:xx:xx"?
Edit: I'm formatting the output date as
public static final SimpleDateFormat DATE_FMT =
new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss zzz");
Try this format (HH instead of kk):
public static final SimpleDateFormat DATE_FMT =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
That information is in the SimpleDateFormat JavaDoc.
Joda’s formatter has similar pattern format.

Resources