Skip to content

Instantly share code, notes, and snippets.

Mohammad Abbas mabbas007

Block or report user

Report or block mabbas007

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mabbas007
mabbas007 / String+extensions
Created Jul 25, 2018
Youtube duration parsing in swift
View String+extensions
extension String {
/// Converts an ISO 8601 formatted `String` into `NSDateComponents`.
///
/// - Note: Does not accept fractional input (e.g.: P3.5Y), must be integers (e.g.: P3Y6M).
/// - SeeAlso: https://en.wikipedia.org/wiki/ISO_8601#Durations
/// - Returns: If valid ISO 8601, an `NSDateComponents` representation, otherwise `nil`.
func ISO8601DateComponents() -> NSDateComponents? {
// Regex adapted from Moment.js https://github.com/moment/moment/blame/develop/src/lib/duration/create.js#L16
let pattern = "^P(?:(\\d*)Y)?(?:(\\d*)M)?(?:(\\d*)W)?(?:(\\d*)D)?(?:T(?:(\\d*)H)?(?:(\\d*)M)?(?:(\\d*)S)?)?$"
let nsstringRepresentation = self as NSString
You can’t perform that action at this time.