I am having some trouble in parsing this date ("APR 19, 3:15p ET"), can anyone please help me in this regard?
This is the code which I have written.
d, err := time.Parse("JAN 02, 3:04p ET", date)
Thanks in advance.
The 3 letter month constant is Jan
date := "APR 19, 3:15p ET"
d, err := time.Parse("Jan 02, 3:04p ET", date)
http://play.golang.org/p/LkeQOtc1Hd
Related
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.
i have this dates
"25th April 2019 01:01 AM"
"11th May 2019 07:28 AM"
"26th August 2019 11:07 AM"
"31st July 2019 01:26 PM"
ETC...
my try
timeStr = strings.Replace(timeStr,"th","",1)
timeStr = strings.Replace(timeStr,"st","",1)
timeStr = strings.Replace(timeStr,"rd","",1)
timeStr = strings.Replace(timeStr,"nd","",1)
time.Parse("2 January 2006 15:04 PM",timeStr)
but this is wrong as it can remove characters from the month
Can use a regexp to do such kind of things.
re := regexp.MustCompile(`^(\d{1,2})(th|st|rd|nd)`)
re.ReplaceAllString("31st July 2019 01:26 PM", "$1")
How about this?
if d := timeStr[1]; d >= '0' && d <= '9' {
// 2-digit day
timeStr = timeStr[:2] + timeStr[4:]
} else {
// 1-digit day
timeStr = timeStr[:1] + timeStr[3:]
}
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;
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.
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.")
}