How to get timestamp from a String like "2017-08-30T06:40:00.000+00:00" in Swift 3?

346 views Asked by At

enter image description here

consider the string like "2017-08-30T06:40:00.000+00:00" 1 - How to convert this string into timestamp 2 - How to group this value into time slots (for eg, if the value is time = 2:30 PM, it will be grouped into the slot of 2pm to 3pm)

1

There are 1 answers

5
Deepakraj Murugesan On

I will help you with an idea in objective C try converting to the swift, If not you can ask me. I will help you out with the right code. Write a function like this generically and use it in the places wherever you wanted to convert the date format.

+ (NSString*)dateFormatConv:(NSString *)givenFormat DateValue:(NSString*)dateString needFormat:(NSString *)reqFormat LabelName:(UILabel *)lbl{
    NSString *myDateString = dateString;

    NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
    [DateFormatter setDateFormat:givenFormat];

    NSDate *date = [DateFormatter dateFromString:myDateString];
    [DateFormatter setDateFormat:reqFormat];
    NSString *dateString2 = [DateFormatter stringFromDate:date];
    lbl.text = dateString2;
    return dateString2;
}