Subtracting datetime from google admin console logs - google-sheets

Google admin exports the following Date Time stamps which are not recognized by sheets. How can I get sheets to recognize these as date format? So, I can complete subtraction on them like "Feb 3, 2020, 2:00:29 PM GMT" minus "Feb 2, 2020, 1:00:09 PM GMT"

Workaround
I'll use these datetimes as example Feb 3, 2020, 2:00:29 PM GMT and Feb 2, 2020, 3:00:09 PM GMT
At the moment there's no sheet function which handles timezone so here's my approach:
First separate the days like so:
=TEXT(INDEX(SPLIT(A2, ","),1 ,1), "mmm d, ") & INDEX(SPLIT(A2, ","),1 ,2)
As well as the hours:
=SUBSTITUTE(INDEX(SPLIT(A5, ","), 1, 3), "GMT", "")
Then simply calculate the difference in hours:
Date difference in hours
=DATEVALUE(TEXT(INDEX(SPLIT(A2, ","),1 ,1), "mmm d, ") & INDEX(SPLIT(A2, ","),1 ,2)) -
DATEVALUE(TEXT(INDEX(SPLIT(B2, ","),1 ,1), "mmm d, ") & INDEX(SPLIT(B2, ","),1 ,2))
DATEVALUE Converts a provided date string in a known format to a date value which is a number that we can treat.
NOTE: DAYS function will treat leap years as well so you can use it instead of DATEVALUE difference
Hour difference
=TIMEVALUE(SUBSTITUTE(INDEX(SPLIT(A2, ","), 1, 3), "GMT", "")) -
TIMEVALUE(SUBSTITUTE(INDEX(SPLIT(B2, ","), 1, 3), "GMT", ""))
TIMEVALUE will return the fraction of a 24-hour day which can be negative, I won't use =ABS function because I want to add this value to the date difference, so in adding this negative value we'll make the real difference between 2 dates.
Once we have both values we simply add them in order to retrieve the difference in hours:
=( DATEVALUE(TEXT(INDEX(SPLIT(A2, ","),1 ,1), "mmm d, ") & INDEX(SPLIT(A2, ","),1 ,2)) -
DATEVALUE(TEXT(INDEX(SPLIT(B2, ","),1 ,1), "mmm d, ") & INDEX(SPLIT(B2, ","),1 ,2)) ) * 24
+
(TIMEVALUE(SUBSTITUTE(INDEX(SPLIT(A2, ","), 1, 3), "GMT", "")) -
TIMEVALUE(SUBSTITUTE(INDEX(SPLIT(B2, ","), 1, 3), "GMT", "")) ) * 24
And we can retrieve different formats like hours or minutes keeping in mind that DATEVALUE and TIMEVALUE have to be multiplied by 24.
References
TEXT
SPLIT
INDEX
SUBSTITUTE
TIMEVALUE
DATEVALUE
DAYS: not used in my approach but it's useful.

Related

Store date in neo4j

The given date format in CSV is '(Fri) 09 Jan 2018 (32)'. This should feed to database as a date column to allow order by date. How could convert above format to Neo4j date format at the insertion time ?
Solution
WITH '(Fri) 09 Jan 2018 (32)' as inputString
WITH split(inputString, ' ') as parts
WITH parts[1] + ' ' + parts[2] + ' ' + parts[3] AS concatDate
RETURN apoc.date.parse(concatDate, 's',"dd MMM yyyy") as date;
Explanation:
line 1: defines a date for testing purpose
line 2: splits the given date into its pieces at each blank
line 3: concatenates the day, month and year
line 4: parse the built date of line 3 and convert it to a Neo4j date
Result
╒══════════╕
│"date" │
╞══════════╡
│1515456000│
└──────────┘
Alternative solution
WITH '(Fri) 09 Jan 2018 (32)' as inputString
WITH split(inputString, ' ') as parts
WITH reduce(s = "", x IN parts[1..4] | s + x) AS concatDate
RETURN apoc.date.parse(concatDate, 's',"ddMMMyyyy") as date;
Assuming the date substring always starts at offset 6 in the input string, this will return the date offset from 01 Jan 1970, which is adequate for comparing dates:
WITH '(Fri) 09 Jan 2018 (32)' as s
RETURN apoc.date.parse(SUBSTRING(s, 6, 11), 'd', "dd MMM yyyy") as date;

How to format a date range (with only one year string) in different locale's in iOS?

The date string in English: Jan 18 - Jan 26, 2018
Incorrect Korean date string: Jan 18 - 2018 Jan 26
What should happen in Korean: 2018 Jan 18 - Jan 26 (not exactly correct Korean, just referring to the location of the year. See accepted answer to see proper Korean date format)
Right now this requires to date formatters, but you have to hardcode which date formatter has the year, so the Korean date doesn't look right.
Is this possible to do in Swift/Objc without just putting the year string on both sides of the date range?
Use a DateIntervalFormatter:
let sd = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 18))!
let ed = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 26))!
let dif = DateIntervalFormatter()
dif.dateStyle = .medium
dif.timeStyle = .none
dif.locale = Locale(identifier: "en_US")
let resEN = dif.string(from: sd, to: ed)
dif.locale = Locale(identifier: "ko_KR")
let resKO = dif.string(from: sd, to: ed)
This results in:
Jan 18 – 26, 2018
2018. 1. 18. ~ 2018. 1. 26.
The output isn't exactly what you show in your question but the output is appropriate for the given locales.

Ruby on Rails - Get date - given is year, month, weeknumber and day

I have some troubles with the following task:
Given are the following parameters:
Year: 2016
Month: 2
Week of Month: 2
Day of week: Monday
The result should be 2016-02-08
Is there a method to get this?
Thanks in advance
A way to do this would be
weekday_index = {monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 7}
day_in_a_week = 7
year = year_param # 2016
month = month_param # 8
week_number = 2
day_of_month = (week_number * days_in_a_week) - (days_in_a_week - weekday_index[:monday])
Date.new(year, month, day_of_month).to_s
You can use this code. Values used were supplied in the question month = 2, week_number = 2 and year = 2016
month = 2
year = 2016
week_number = 2
FIRST_DAY = 1 #first day of every month
day = { monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 7}
first_wk_of_month = Date.new(year, month, FIRST_DAY).cweek #=> 05
date_week_num = first_wk_of_month + (week_number - 1) #=> 06
date = Date.commercial(year, date_week_num, day[:monday]).to_s # 2016-02-08
There are 52 weeks in a year and a month can have as low as 1 day in a week if it starts on Sunday. Taking that into consideration,
first_wk_of_month finds the week number in the year (between 1 and 52) on which the month in question starts.
date_week_num finds the actual week number in the year the date falls on. In this scenario which is 2nd week of February it returns 6 representing the 6th week of the year
Passing the date_week_num and the day to the Date.commercial allows us to find the date
Tested it with a couple of days and works fine. You could probably change the variable names. Hope this solves your challenge

NSComparisonResult is not working as expected for 10 PM and above

I need to compare two times in Swift and using NSComparisonResult I could get correct result until it comes to time between 10 PM - 11:59 PM. It shows opposite result for these times. Anyone know what's the issue with this? Below is sample code and scenario's. 10:30:00 PM is example time to test, but you can test it with any time.
// For test, Current time 10:30:00 PM
let currentTime = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .NoStyle, timeStyle: .LongStyle)
let closeTimeCompareResult: NSComparisonResult = currentTime.compare("10:00:00 PM EDT")
print("DinnerClose: \(closeTimeCompareResult.rawValue)")
// Expected result is -1 but, getting as 1
// It works perfect until 9:59:59 PM
let closeTimeCompareResult9: NSComparisonResult = currentTime.compare("9:00:00 PM EDT")
print("DinnerClose: \(closeTimeCompareResult9.rawValue)")
// As expected result is -1
You're performing a string comparison. So you're comparing these two strings, for example:
10:00:00 PM EDT
9:00:00 PM EDT
A string comparison compares the corresponding characters of each string, starting with the first character of each. The first character of "10:00:00 PM EDT" is "1" and the first character of "9:00:00 PM EDT" is "9". In Unicode and ASCII, "9" is code point 57 and "1" is code point 49. Since 57 > 49, "9" > "1", and "9:00:00 PM EDT" > "10:00:00 PM EDT".
You probably want to extract the hour, minute, and second from the input date, and then compare them numerically. If you've upgraded to Xcode 7.3 with Swift 2.2, then you can use a tuple comparison like this:
let date = NSDate()
let components = NSCalendar.currentCalendar().components([.Hour, .Minute, .Second], fromDate: date)
let hms = (components.hour, components.minute, components.second)
if hms >= (21, 0, 0) && hms < (22, 30, 0) {
print("\(date) is between 9 PM and 10:30 PM in the system's time zone.")
}

Converting Date+Time Formats

I need to convert the following format into a new format using NSDateFormatter.
'Fri, 25 Mar 2011 10:16:00 -0700'.
I tried using "aaa, dd bbb YYYY HH:MM:SS ZHHMM" as format, but it doesn't work; it gives me a date way in the past.
I also need to convert it into the Eastern Time Zone when creating a new date.
The code I used is the following one:
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc] init];
[newDateFormatter setDateFormat:#"dd MMM YYYY HH:mm:SS"];
Let's break this string down into the various portions:
Fri, 25 Mar 2011 10:16:00 -0700 becomes:
Fri: abbreviated day of the week
25: potentially zero-padded day of the month
Mar: abbreviated month
2011: year
10: potentially zero-padded hour (in 24-hour format because there is no AM/PM)
16: potentially zero-padded minute
00: zero-padded second
-0700: time zone offset
Now let's look at the date format patterns that NSDateFormatter supports:
abbreviated day of the week: EEE
zero-padded day of the month: dd
abbreviated month: MMM
year: y
zero-padded hour (24-hour): HH
zero-padded minute: mm
zero-padded second: ss
time zone offset: ZZZ
Thus, your format string should be: #"EEE, dd MMM y HH:mm:ss ZZZ".

Resources