//
Timetransform.m
Timeformatdemo
//
Created by code Fix on 15/8/16.
Copyright (c) 2015 yards of practitioners. All rights reserved.
//
#import "TimeTransform.h"
#define SET_TIME_NORMAL_FORMATTER \
nsdateformatter* formatter = [[NSDateFormatter alloc] Init];\
[Formatter setdatestyle:nsdateformattermediumstyle];\
[Formatter settimestyle:nsdateformattershortstyle];\
[Formatter setdateformat:@ "YYYY-MM-DD"];
@implementation Timetransform
#pragma mark-public
/** the month of a string of 2015-05-20 format into the English month
*
* @param string in time 2015-05-20 format
*
* @param abbreviations whether to use a date abbreviation for month abbreviations
*
* @return English format date (optional) */
+ (NSString *) englishmonthwithtimestring: (NSString *) timenormal abbreviations: (BOOL) abbreviations Englishshortnamefordate: (BOOL) Englishshortnamefordate {
if (timenormal.length! = 10) {
NSLog (@ "Time format error");
return nil;
}
return [self substingofymd:timenormal abbreviations:abbreviations englishshortnamefordate:englishshortnamefordate] ;
}
/** converts a timestamp to a string in the 2015-05-20 format, removes 0, and then turns its month into the English month
*
* @param string in time 2015-05-20 format
*
* @param abbreviations whether to use a date abbreviation for month abbreviations
*
* @return English format date (optional) */
+ (NSString *) Englishmonthwithtimestamp: (NSString *) TimeStamp abbreviations: (BOOL) abbreviations Englishshortnamefordate: (BOOL) englishshortnamefordate
{
set_time_normal_formatter;//setting the standard format YYYY-MM-DD
NSDate *changedate = [NSDate datewithtimeintervalsince1970:[timestamp doublevalue];
NSString *time = [Formatter stringfromdate:changedate];
if (time.length! = 10) {
NSLog (@ "Time format error");
return nil;
}
return [self substingofymd:time abbreviations:abbreviations englishshortnamefordate:englishshortnamefordate];
}
/** converts a nsdate to a string in the 2015-05-20 format, removes 0, and converts its month to the English month
*
* @param string in time 2015-05-20 format
*
* @param abbreviations whether to use a date abbreviation for month abbreviations
*
* @return English format date (optional) */
+ (NSString *) Englishmonthwithdate: (nsdate *) Date abbreviations: (bool) Abbreviations englishshortnamefordate: (BOOL) englishshortnamefordate{
set_time_normal_formatter;//setting the standard format YYYY-MM-DD
NSString *timenormal = [Formatter stringfromdate:date];
if (timenormal.length! = 10) {
NSLog (@ "Time format error");
return nil;
}
return [self substingofymd:timenormal abbreviations:abbreviations englishshortnamefordate:englishshortnamefordate] ;
}
#pragma mark-private
+ (NSString *) SUBSTINGOFYMD: (NSString *) time abbreviations: (bool) Abbreviations englishshortnamefordate: (BOOL) englishshortnamefordate{
Each day of the year is intercepted
Month
Nsrange range;
Range.length = 2;
Range.location = 5;
NSString * a = [time substringwithrange:range];
int AA = [a intvalue];
Nsarray * array = nil;
if (abbreviations) {//whether the month abbreviation is used
Array = @[@ "Jan", @ "Feb", @ "Mar", @ "APR", @ "may", @ "June", @ "Jul", @ "in", @ "Sep" @ "Oct" @ "Nov" @ "Dec"];
} else {
Array = @[@ "January", @ "February", @ "March", @ "April", @ "may", @ "June", @ "July", @ "August", @ "September", @ "October", @ "No Vember ", @" December "];
}
NSString * timestr = time;
Day
NSString * month = Array[aa-1];
Nsrange Rangeday;
Rangeday.length = 2;
Rangeday.location = 8;
NSString * day = [Time substringwithrange:rangeday];
Year
Nsrange rangeyear;
Rangeyear.length = 4;
rangeyear.location = 0;
NSString * year = [time substringwithrange:rangeyear];
if (englishshortnamefordate) {//whether to use date abbreviations
if ([Day Intvalue] > 9) {
TIMESTR = [NSString stringwithformat:@ "%@% @th", month, day];
} else if ([day intvalue] = = 1) {
Day = [Day stringbyreplacingoccurrencesofstring:@ "0" withstring:@ ""];
TIMESTR = [NSString stringwithformat:@ "%@% @st", month, day];
} else if ([day intvalue] = = 2) {
Day = [Day stringbyreplacingoccurrencesofstring:@ "0" withstring:@ ""];
TIMESTR = [NSString stringwithformat:@ "%@% @nd", month, day];
} else if ([day intvalue] = = 3) {
Day = [Day stringbyreplacingoccurrencesofstring:@ "0" withstring:@ ""];
TIMESTR = [NSString stringwithformat:@ "%@% @rd", month, day];
} else {
Day = [Day stringbyreplacingoccurrencesofstring:@ "0" withstring:@ ""];
TIMESTR = [NSString stringwithformat:@ "%@% @th", month, day];
}
Time = [NSString stringwithformat:@ "%@,%@", timestr,year];
}else {
Time = [NSString stringwithformat:@ "%@%@,%@", month,day,year];
}
return time;
}
@end
Small white on the road iOS timestamp, character type, date type time conversion to English format