I'm extracting strings from a text that represent dates. They look like this:
Monday August 16, 2010 05:28 AM EST
I'm trying to parse them with a NSDateFormatter. I've set its format to:
[dateFormatter setDateFormat:#"EEEE MMMM d, YYYY h:mm a z"];
However, this doesn't work. For the example I gave above, if I convert the string to a date and then that date to a string, the date formatter returns this:
Monday December 28, 2009 5:28 AM EST
What am I doing wrong?
EDIT: It seems to work when using the format [dateFormatter setDateFormat:#"EEEE MMMM d, y h:mm a z"];. Strange...
Will,
NSDateFormatter uses the Unicode standard for formatting dates. As you've found, "y" works, but "YYYY" doesn't. If you see the spec (referenced several layers deep in the Apple documentation, so not easy to find) you'll see that "Y" has this note: "Year (of "Week of Year"), used in ISO year-week calendar. May differ from calendar year."
Here is the link to the specification.
I know you've fixed it yourself, but this may help explain why "Y" didn't work for you.
"y" or "yyyy" is ok. "YYYY" doesn't work.
Check the following link for detail.
situee.blogspot.com: NSDateFormatter setDateFormat YYYY return wrong year
Related
In a Power Automate / MSFlow flow, I receive a string like this:
Saturday, July 31, 2021 1:35 AM | (UTC+01:00) Dublin, Edinburgh, Lisbon, London
I need to convert it to the format Sharepoint expects, i.e. a ISO 8601 for UTC:
YYYY-MM-DDTHH:MM:SS
I painfully built a flow that does most of the work; however, at the thought of having to manually manage the timezone offset my heart finally gave out. It's shocking that in 2021 a product like this cannot parse a datetime like we could do in any language in 1995.
So my question is: what's the easiest way to parse this datetime correctly in PA/Flow...?
I followed the MS reference:
Workflow definition language functions reference: formatDateTime
And the way to go was with "formatDateTime" function:
formatDateTime('<timestamp>', '<format>'?)
But, before that, the timestamp should be reformated in your case.
My approach will be the following:
Let's assume that the input string will be stored in a variable named timestamp_original.
Take the part before "|" character from the timestamp_original. and remove the Day (at the beginning) from the result.
pass the result to the formatDateTime function.
Use the convertTimeZone function to achieve the TimeZone conversion. (but this part is not working as I expected).
So the final result should be obtained by using the next expressions:
convertTimeZone(formatDateTime(first( split( substring(variables('timestamp_original'), add(2, indexOf(variables('timestamp_original'),','))) , '|' ) )), 'Central Europe Standard Time', 'Greenwich Standard Time')
The solution to the point 3, should be extracting the TimeZone from the timestamp_original string:
trim(first(split(last( split(variables('timestamp_original'), '(' ) ), ')')))
And add some logic to test if this TimeZone contains "UTC+" or "UTC-" and add or extract hours from the obtained timestamp.
I think you can do this.
It should be noted that the variable 'timestamp_original' should be initially in the next format (as the OP mentionned):
Saturday, July 31, 2021 1:35 AM | (UTC+01:00) Dublin, Edinburgh, Lisbon, London
So the result will be: (using the convertTimeZone function)
2021-07-30T23:35:00.0000000
And without using the convertTimeZone function:
2021-07-31T01:35:00.0000000
I hope this has helped. Good luck.
When using NSDateFormatter with doesRelativeDateFormatting turned on, all relative formatting seems to be relative to the current system time. I would like to format dates with relative formatting, such as "Today" or "Tomorrow", but relative to an arbitrary "current" time instead of the actual time.
For example, I would like to specify an NSDate of November 2, 2015, with November 1, 2015 as the "current" time, and have it formatted as "Tomorrow" instead of "November 2, 2015". Can NSDateFormatter do this?
it is always relative to the system date. sorry.
BUT
there is https://github.com/billgarrison/SORelativeDateTransformer that might be able to do this. maybe you can get code from there
I want to get the current date and time as example date: 11/10/2014 and time 8:30 am or 6:00 pm and pass it as parameters to my Jmeter test. Can some help me do this.
Use __time function:
${__time(dd/MM/yyyy,)}
${__time(hh:mm a,)}
Since JMeter 3.3, there are two new functions that let you compute a time:
__timeShift
"The timeShift function returns a date in the given format with the specified amount of seconds, minutes, hours, days or months added" and
__RandomDate
"The RandomDate function returns a random date that lies between the given start date and end date values."
Since JMeter 4.0:
dateTimeConvert
Convert a date or time from source to target format
If you're looking to learn jmeter correctly, this book will help you.
it seems to be the java SimpleDateFormat : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
here are some tests i did around 11:30pm on the 20th of May 2015
${__time(dd-mmm-yyyy HHmmss)} 20-032-2015 233224
${__time(d-MMM-yyyy hhmmss)} 20-May-2015 113224
${__time(dd-m-yyyy hhmmss)} 20-32-2015 113224
${__time(D-M-yyyy hhmmss)} 140-5-2015 113224
${__time(DD-MM-yyyy)} 140-05-2015
JMeter is using java SimpleDateFormat
For UTC with timezone use this
${__time(yyyy-MM-dd'T'hh:mm:ssX)}
Use ${__time(yyyy-MM-dd'T'hh:mm:ss)} to convert time into a particular timeformat.
Here are other formats that you can use:
yyyy/MM/dd HH:mm:ss.SSS
yyyy/MM/dd HH:mm:ss
yyyy-MM-dd HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss
MM/dd/yy HH:mm:ss
You can use Z character to get milliseconds too.
For example:
yyyy/MM/dd HH:mm:ssZ => 2017-01-25T10:29:00-0700
yyyy/MM/dd HH:mm:ss.SSS'Z' => 2017-01-25T10:28:49.549Z
Most of the time yyyy/MM/dd HH:mm:ss.SSS'Z' is required in some APIs. It is better to know how to convert time into this format.
Actually, for UTC I used Z instead of X, e.g.
${__time(yyyy-MM-dd'T'hh:mm:ssZ)}
which gave me:
2017-09-14T09:24:54-0400
Use this format:
${__time(yyyy-MM-dd'T'hh:mm:ss.SS'Z')}
Which will give you:
2018-01-16T08:32:28.75Z
Should have double quotes surrounding the ${}
String todaysDate = "${__time(yyyy-MM-dd'T'HH:mm:ss.SSS'Z')}";
In dealing with a wide variety of JSON data being sent from various clients date format standardization is a real problem.
I might get any of these:
2013-10-05
2-6-13
Mon, Jul 13 2013
Sometimes there's hours, minutes and seconds as well as time zone information. For each client I've had to manually set up an NSDateFormatter, or if unable to get it parsing I've done substring searching. What I'd really like to do is get all this combined into a single method and somehow determine which type I'm dealing with. Then have some kind of switch statement that handles the parsing for that particular date.
I'm unable to change their formats, but how can I deal with them better?
This is not exactly the answer to your question but when I'm looking to parse dates without being sure of the format I usually go for NSDataDetector setup for dates only. The detector will often detect dates in multiple languages and format all at once. It's pretty neat.
Okay thanks for the comments. What I ended up doing is going to http://waracle.net/iphone-nsdateformatter-date-formatting-table/ and studying VERY CAREFULLY all the NSDateFormatter options. In particular it was the time zone +0000 at the end of RSS posts which was messing up my format conversions. RSS dates match RFC 822, which means you need to use THREE Z's at the end to properly catch it.
I was attempting things like +ZZZZ which were failing on the strings. That's why I was trimming the strings before. What ended up working was this:
#"Wed, 17 Jul 2013 03:23:18 +0000"
Needs formatter of:
#"EEE, dd MMM yyyy HH:mm:ss ZZZ"
After having correctly set up the DateFormatter for this case I was easily able to figure out others for the other formats. It turns out there's only 3 different date formats being used in the projects and I can easily store the right date format string along side each data source and plug that into the date formatter when needed. No more string searching needed and all data is cleanly converting to the correct NSDate.
So the answer is, make a sample project that converts various date formats, plug in the ones you need and use the DateFormatter codes correctly. Chances are your date problems aren't as bad as you think and a better understanding of them can lead to cleaner code.
Using my example below you will see (null) if your format string is incorrect.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSArray *dateStrings = #[
// From data source 1.
#[#"2012-04-18",#"yyyy-MM-dd"],
// From data source 2.
#[#"2012-04-19 18:29:35",#"yyyy-MM-dd HH:mm:ss"],
#[#"2011-05-04 11:10:50",#"yyyy-MM-dd HH:mm:ss"],
// From RSS feeds (pubDate)
#[#"Wed, 17 Jul 2013 02:24:23 +0000",#"EEE, dd MMM yyyy HH:mm:ss ZZZ"],
#[#"Tue, 25 Jun 2013 15:04:49 +0000",#"EEE, dd MMM yyyy HH:mm:ss ZZZ"]
];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
for(NSArray *datePair in dateStrings){
[formatter setDateFormat:datePair[1]];
NSDate *thisDate = [formatter dateFromString:datePair[0]];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSLog(#"%# = %#",datePair[0],[formatter stringFromDate:thisDate]);
}
}
return 0;
}
I'm looking at the PayPal IPN docs, and it says the datetime stamps of their strings are formatted as:
HH:MM:SS DD Mmm YY, YYYY PST
So year is specified twice?
Once in double digits, and another with 4 digits?
This looks bizarre.
This seems to be a bug in the documentation. The actual format should be "HH:MM:SS Mmm DD, YYYY PST" (e.g. "08:30:06 Apr 19, 2017 PDT")
Actually in PHP you need to use date("Y-m-d\TH:i:s\Z") . That will result in something that looks like 2012-04-30T00:05:47Z -- I didn't notice a difference between urlencoded and non.
Where are you guys finding this info? This information is elusive in their documentation and cost me an hour or two of hunting and trying stuff. The only place I see this format is in the TIMESTAMP field. Having a hard time with the PayPal NVP API's PROFILESTARTDATE for CreateRecurringPaymentsProfile and a "Subscription start date should be valid" error.
For php, the syntax is date("G:i:s M m, Y T");
this is the correct format according to their documentation - 2010-03-27T12:34:49Z
so it is - YYYY-MM-DDTHH:MM:SSZ (I don't know what the T in the middle and Z is but it's constant for all the dates)
I've created PayPal NVP library in Java, so if you want to check how it works, or use it,
you are more than welcome. it's on sourceforge - payapal-nvp.sourceforge.net
Complete date plus hours, minutes, seconds and a decimal fraction of a
second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
Where TZD = time zone designator (Z or +hh:mm or -hh:mm)
Example
1994-11-05T08:15:30-05:00 corresponds to November 5, 1994, 8:15:30 am, US Eastern Standard Time.
1994-11-05T13:15:30Z corresponds to the same instant.
https://www.w3.org/TR/NOTE-datetime
PayPal Format to Any format 100% working and easy copy Paste
$payPalFormat = "18:30:30 Feb 28, 2008 PST";
$subrotoFormat = date('Y-m-d', strtotime($payPalFormat));
Result: 2008-02-29
All Format: https://www.w3schools.com/php/func_date_date.asp
https://gist.github.com/subrotoice/d820863ce65eb0d8434a47a76d005df4 (Subroto Biswas Gist)
Actually, I think the right format is: yyyy-MM-ddTHH:MM:ssZ
The case is important.