stringFromDate returning the wrong year - nsdate

#define stdDateFormat #"YYYYMMdd'T'hh:mm:ssZ"
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:stdDateFormat];
NSString *TimeOfSync = [dateFormat stringFromDate:syncDate];
NSLog(#"date format: %#",stdDateFormat);
NSLog(#"syncDate: %#",syncDate);
NSLog(#"TimeOfSync: %#",TimeOfSync);
Logged output:
date format: YYYYMMdd'T'hh:mm:ssZ
syncDate: 2009-01-03 19:00:00 +0000
TimeOfSync: 20080103T11:00:00-0800
Can anyone help? The only thing I can see is the extra space just before the timezone in the input date string. Thanks.

Use lowercase yyyy for the year instead of YYYY.
Uppercase Y means the year of the start of the week the date is in (Jan 3, 2009 is in a week that starts in 2008).
See Unicode Date Format Patterns.

Related

Converting integer to time format in excel

Date
19112018
19112016
19112015
19112013
I have a column named Date.
I want to convert 19112018 to 2019-11-20 18:00
19 means year 2019
11 month
20 days
18 is hour
Thanks .
Use the following formula:
=DATE(20&LEFT(A2,2),MID(A2,3,2),MID(A2,5,2))+RIGHT(A2,2)/24
Or:
=--(20&REPLACE(REPLACE(REPLACE(A2,7,0," "),5,0,"-"),3,0,"-")&":00")
Then format the output cell:
yyyy-mm-dd hh:mm

How to extract or validate date format from a text using python?

I'm trying to execute this code:
import datefinder
string_with_dates = 'The stock has a 04/30/2009 great record of positive Sept 1st, 2005 earnings surprises, having beaten the trade Consensus EPS estimate in each of the last four quarters. In its last earnings report on May 8, 2018, Triple-S Management reported EPS of $0.6 vs.the trade Consensus of $0.24 while it beat the consensus revenue estimate by 4.93%.'
matches = datefinder.find_dates(string_with_dates)
for match in matches:
print(match)
The output is:
2009-04-30 00:00:00
2005-09-01 00:00:00
2018-05-08 00:00:00
2019-02-04 00:00:00
The last date has come due to the percentage value 4.93% ... How to overcome this situation?
I cannot fix the datefinder module issue. You stated that you needed a solution, so I put this together for you. It's a work in progress, which means that you can adjusted it as needed. Also, some of the regex could have been consolidated, but I wanted to break them out for you. Hopefully, this answer helps you until you find another solution that works better for your needs.
import re
string_with_dates = 'The stock has a 04/30/2009 great record of positive Sept 1st, 2005 earnings surprises having beaten the trade Consensus EPS estimate in each of the last ' \
'four quarters In its last earnings report on March 8, 2018, Triple-S Management reported EPS of $0.6 vs.the trade Consensus of $0.24 while it beat the ' \
'consensus revenue estimate by 4.93%. The next trading day will occur at 2019-02-15T12:00:00-06:30'
def find_dates(input):
'''
This function is used to extract date strings from provide text.
Symbol references:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
:param input: text
:return: date string
'''
date_formats = [
# Matches date format MM/DD/YYYY
'(\d{2}\/\d{2}\/\d{4})',
# Matches date format MM-DD-YYYY
'(\d{2}-\d{2}-\d{4})',
# Matches date format YYYY/MM/DD
'(\d{4}\/\d{1,2}\/\d{1,2})',
# Matches ISO 8601 format (YYYY-MM-DD)
'(\d{4}-\d{1,2}-\d{1,2})',
# Matches ISO 8601 format YYYYMMDD
'(\d{4}\d{2}\d{2})',
# Matches full_month_name dd, YYYY or full_month_name dd[suffixes], YYYY
'(January|February|March|April|May|June|July|August|September|October|November|December)(\s\d{1,2}\W\s\d{4}|\s\d(st|nd|rd|th)\W\s\d{4})',
# Matches abbreviated_month_name dd, YYYY or abbreviated_month_name dd[suffixes], YYYY
'(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept|Oct|Nov|Dec)(\s\d{1,2}\W\s\d{4}|\s\d(st|nd|rd|th)\W\s\d{4})',
# Matches ISO 8601 format with time and time zone
# yyyy-mm-ddThh:mm:ss.nnnnnn+|-hh:mm
'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\+|-)\d{2}:\d{2}',
# Matches ISO 8601 format Datetime with timezone
# yyyymmddThhmmssZ
'\d{8}T\d{6}Z',
# Matches ISO 8601 format Datetime with timezone
# yyyymmddThhmmss+|-hhmm
'\d{8}T\d{6}(\+|-)\d{4}'
]
for item in date_formats:
date_format = re.compile(r'\b{}\b'.format(item), re.IGNORECASE|re.MULTILINE)
find_date = re.search(date_format, input)
if find_date:
print (find_date.group(0))
find_dates(string_with_dates)
# outputs
04/30/2009
March 8, 2018
Sept 1st, 2005
2019-02-15T12:00:00-06:30

Trouble creating a custom date format

I am trying to build a custom date format that matches the format below. I've included an example of the number value (date) and how the output should look. I've tried something like: ddd mmm d, yyyy h:mm:ssAM/PM [ddd mmm d, yyyy h:mm:ssAM/PM] as a custom format, but can't seem to get it to work. Can you tell me where I am going wrong? Thank you in advance!
(Date time) 1546926706 = (Formatted Output) Mon Jan 7, 2019 9:51:46pm PST [Mon Jan 7, 2019 10:51:46pm]
Attempt at translating format into custom format Excel understands:
`
ddd mmm d, yyyy h:mm:ssAM/PM [ddd mmm d, yyyy h:mm:ssAM/PM]
Basically I'm trying to reverse engineer a custom date format because I can't access the system that is sending me the data and need it to make sense on for users on the front end.
`
1546926706 is UNIX/POSIX time where each 1 is a second after at 1-Jan-1970. You need a conversion to an excel datetime where each 1 is a day that starts 1-Jan-1900.
'Tue Jan 8, 2019 5:51:46 AM PST
=TEXT(1546926706/86400 + 25569, "ddd mmm d, yyyy h:mm:ss am/pm \P\S\T")
That's returns text, not a true datetime but it shows you the conversion and format mask.

How to store NSDate plus 7 calendar days?

I need to be able to store the current NS Date plus 7 days. I already store the current NSDate. I then want to compare the two dates, to get the number of days difference between the two and display within a label. The idea is to count down from 7 days, to 6 days, to 5 days, etc until it shows 0 and i will remove it from my tableview.
I have been having hard time finding the write code for this, so any help would be greatly appreciated!!!
This code will compute date after 7 days from today.
NSDateComponents *comps = [NSDateComponents new];
comps.day = 7;
NSDate *today = [NSDate date];
NSCalendar *c = [NSCalendar currentCalendar];
NSDate *dateAfter7days = [c dateByAddingComponents:comps toDate:today options:0];
For Your date diffrence the code is:-
// c is calendar object
NSDateComponents *comps = [c components:NSDayCalendarUnit fromDate:startDate toDate:currentDate options:0];
int diffrence = [comps day];

NSPredicate and CoreData - decide if a Date attribute is "today" (or between last night 12am to tonight 12am) on iOS

I'm using a NSFetchedResultsController and a UITableViewController to populate a UITableView from a CoreData database.
I have a NSDate object saved into this Date attribute labeled "startTime". Then I'm trying to only pull todays's data by using a NSPredicate that looks like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"startDate == %#",
todaysDate];
I'm getting zero results. I understand this because a NSDate object just hold the number of seconds or milliseconds or whatever since Jan 1 1970 right? So comparing 1111111111111 and 1111111111112, while on the same day, are two distinct NSDate objects that aren't equal.
So, how could the NSPredicate be formatted so that it would do it? I'm going to guess its creating two NSDate objects: one that is at 12am last night, and another for 12am tonight and compare startDate and see if its between these two NSDates.
I'm kind of new to NSPredicate, so how could this be accomplished?
Use NSCompoundPredicate.
NSPredicate *firstPredicate = [NSPredicate predicateWithFormat:#"startDate > %#", firstDate];
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:#"startDate < %#", secondDate];
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:firstPredicate, secondPredicate, nil]];
Where firstDate is 12am this morning and secondDate is 12am tonight.
P.S. Here's how to get 12am this morning:
NSCalendar *calendar = [NSCalendar calendar]; // gets default calendar
NSCalendarComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, and day for today's date
NSDate *firstDate = [calendar dateFromComponents:components]; // makes a new NSDate keeping only the year, month, and day
To get 12am tonight, change components.day.

Resources