时间字符串以及时间戳解析

项目中用到了许多需要处理时间的地方,专门写了一个类来进行处理。多年之后当我再次看到这个东西时希望我可以微笑面对。

  首先是.h文件

#import <Foundation/Foundation.h>

#import "ALUserDetailAccount.h"

#import "ALLMFTiXianListModel.h"

@interface LMFAnalysisTime : NSObject

#pragma mark --将时间转换成星期

+ (NSString *)changeTimeStringToWeekWith:(NSString *)time;

#pragma mark --将时间转换成时分

+ (NSString *)changeTimeStringToHMWith:(NSString *)time;

#pragma mark --根据日期判断是星期几

+ (NSString *)weekdayStringFromDate:(NSString *)time;

#pragma mark --判断给定日期的下一天

+ (NSString *)nextDayTimeStringWith:(NSString *)time;

#pragma mark --判断是不是一周内

+ (BOOL)ifDateInAweekToNow:(NSString *)time;

#pragma mark --判断是不是从当前时间开始的一周内,今天不行

+ (BOOL)ifDateInAweekFromTomorrow:(NSString *)time;

#pragma mark --判断日期是否是从今天开始今天或今天以后的日期

+ (BOOL)ifDateIsAfterToday:(NSString *)time containToday:(BOOL)flag;

#pragma mark --判断是第几月

+ (NSString *)monthStringWith:(NSString *)time;

#pragma mark --返回某一种状态下某个时间段的数据

+ (NSArray *)dataWithState:(NSString *)state andTime:(NSString *)time;

#pragma mark --判断刷新请求的数据是否已经存在了

+ (BOOL)ifDateExistInArray:(NSArray *)array andTimeString:(NSString *)time;

#pragma mark --判断一维数组中数据是否已经存在

+ (BOOL)ifDateExistInOneArray:(NSArray *)array andTimeString:(NSString *)time;

#pragma mark --判断日期是否是今天或者昨天

+ (BOOL)iFDateIsTodayOrYesterdayWith:(NSString *)time;

#pragma mark --判断日期是否是今天

+ (BOOL)ifDateIsToday:(NSString *)time;

#pragma mark --计算年龄

+ (NSString *)changeBirthdayStringToAge:(NSString *)time;

#pragma mark --判断往期数据是否存在了

+ (BOOL)ifExerciseDataInArray:(NSArray *)array andMid:(NSString *)mid;

+ (BOOL)ifExercisePhotoIsExistInArray:(NSArray *)array andExerciseDate:(NSString *)date;

+ (BOOL)ifTogetherExercisePhotoIsExistInArray:(NSArray *)array andExerciseDate:(NSString *)date;

+ (BOOL)ifKnowledgeHasRead:(NSString *)mid;

#pragma mark --将设置的时间转化为闹钟的秒数

+ (NSArray *)changeTimeToSeconds:(NSArray *)timeArray;

#pragma mark --判断银行卡是否重复了

+ (BOOL)ifCardHasExist:(NSArray *)array and:(ALLMFTiXianListModel *)model;

#pragma mark --判断日期是当天的第几天,当天为第一天

+ (NSInteger)indexOfDateFromToday:(NSString *)time;

#pragma mark --将日期转化为星座

+ (NSString *)changeDateToConstellation:(NSString *)time;

#pragma mark --将日期转化为月日

+ (NSString *)changeDateToMonthAndDay:(NSString *)time;

@end

  下面是.m文件

#import "LMFAnalysisTime.h"

#import "YRDateFormater.h"

#import "ALExerciseAllCellModel.h"

#import "ALLMFExerciseTogetherPhotosModel.h"

@class LMFAnalysisTime;

@implementation LMFAnalysisTime

#pragma mark --将时间转换成星期

+ (NSString *)changeTimeStringToWeekWith:(NSString *)time

{

NSString *week;

YRDateFormater *endt = [[YRDateFormater alloc] init];

NSString *nowTime = [endt dateToStringYMD:[NSDate date]];

//第一步,判断是不是同一天2016-03-20  2016-03-21

NSString *temp = [time substringToIndex:10];

if ([temp isEqualToString:nowTime])

{

week = @"今天";

return week;

}

else

{

NSString *tomorrow = [LMFAnalysisTime nextDayTimeStringWith:time];

if ([[tomorrow substringToIndex:10] isEqualToString:nowTime])

{

week = @"昨天";

}

else

{

week = [LMFAnalysisTime weekdayStringFromDate:time];

}

}

return week;

}

#pragma mark --将时间转换成时分

+ (NSString *)changeTimeStringToHMWith:(NSString *)time

{

return [time substringWithRange:NSMakeRange(11, 5)];

/*

NSString *monthAndDay;

YRDateFormater *endt = [[YRDateFormater alloc] init];

NSString *nowTime = [endt dateToStringYMD:[NSDate date]];

//第一步,判断是不是同一天2016-03-20 00:00:00 2016-03-21

NSString *temp = [time substringToIndex:10];

if ([temp isEqualToString:nowTime])

{

monthAndDay = [time substringWithRange:NSMakeRange(11, 5)];

return monthAndDay;

}

else

{

NSString *tomorrow = [LMFAnalysisTime nextDayTimeStringWith:time];

if ([tomorrow isEqualToString:nowTime])

{

//如果是昨天

monthAndDay = [time substringWithRange:NSMakeRange(11, 5)];

}

else

{

//不是昨天

monthAndDay = [time substringWithRange:NSMakeRange(5, 5)];

}

}

return monthAndDay;

*/

}

+ (NSString *)nextDayTimeStringWith:(NSString *)time

{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSDate *inputDate = [formatter dateFromString:time];

NSDate *tomorrow = [NSDate dateWithTimeInterval:60 * 60 * 24 sinceDate:inputDate];

NSString *tomorrowTime = [formatter stringFromDate:tomorrow];

return tomorrowTime;

}

+ (NSString*)weekdayStringFromDate:(NSString *)time {

//判断是否是今天或者昨天

if ([LMFAnalysisTime iFDateIsTodayOrYesterdayWith:time])

{

NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];

[calendar setTimeZone: timeZone];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSDate *inputDate = [formatter dateFromString:time];

NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;

NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];

return [weekdays objectAtIndex:theComponents.weekday];

}

else

{

return [time substringWithRange:NSMakeRange(5, 5)];

}

}

+ (BOOL)ifDateInAweekToNow:(NSString *)time

{

//获取当前时间YYYY-MM-dd

YRDateFormater *formatter = [[YRDateFormater alloc] init];

NSString *nowTime = [formatter dateToStringYMD:[NSDate date]];

for (int i=0; i<7; i++)

{

//通过判断从给定时间的第几天是不是今天来判断一周以内

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSDate *inputDate = [formatter dateFromString:time];

NSDate *tomorrow = [NSDate dateWithTimeInterval:i * 60 * 60 * 24 sinceDate:inputDate];

NSString *tomorrowTime = [formatter stringFromDate:tomorrow];

NSString *temp = [tomorrowTime substringToIndex:10];

//        NSLog(@"%@-------%@----", temp, time);

if ([temp isEqualToString:nowTime])

{

return YES;

}

}

return NO;

}

+ (BOOL)ifDateInAweekFromTomorrow:(NSString *)time

{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSString *tomorrow = [formatter stringFromDate:[NSDate dateWithTimeInterval:60*60*24 sinceDate:[NSDate date]]];

for (int i=0; i<6; i++)

{

NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];

[formatter1 setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSDate *inputDate = [formatter dateFromString:tomorrow];

NSDate *tomorrow1 = [NSDate dateWithTimeInterval:i * 60 * 60 * 24 sinceDate:inputDate];

NSString *tomorrowTime1 = [formatter stringFromDate:tomorrow1];

NSString *temp = [tomorrowTime1 substringToIndex:10];

NSLog(@"----%@-------%@-----", time, temp);

if ([temp isEqualToString:time])

{

return YES;

}

}

return NO;

}

#pragma mark --判断日期是否是从今天开始今天或今天以后的日期

+ (BOOL)ifDateIsAfterToday:(NSString *)time containToday:(BOOL)flag

{

/*today YYYY:mm:dd

time YYYY:mm:dd HH:mm:ss

*/

YRDateFormater *formatter = [[YRDateFormater alloc] init];

NSString *todayDate = [formatter dateToStringYMD:[NSDate date]];

//    NSLog(@"the date of today is : %@", todayDate);

NSInteger nowY = [[todayDate substringToIndex:4] integerValue];

NSInteger nowM = [[todayDate substringWithRange:NSMakeRange(5, 2)] integerValue];

NSInteger nowD = [[todayDate substringWithRange:NSMakeRange(8, 2)] integerValue];

NSInteger timeY = [[time substringToIndex:4] integerValue];

NSInteger timeM = [[time substringWithRange:NSMakeRange(5, 2)] integerValue];

NSInteger timeD = [[time substringWithRange:NSMakeRange(8, 2)] integerValue];

//    NSLog(@"year:%li month:%li day:%li", nowY, nowM, nowD);

//    NSLog(@"year:%li month:%li day:%li", timeY, timeM, timeD);

if (timeY < nowY)

{

return NO;

}

else

{

if (timeY == nowY)

{

if (timeM < nowM)

{

return NO;

}

else

{

if (timeM == nowM)

{

if (flag)

{

if (timeD < nowD)

{

return NO;

}

return YES;

}

else

{

if (timeD <= nowD)

{

return NO;

}

return YES;

}

}

else

{

return YES;

}

}

}

else

return YES;

}

}

+ (NSString *)monthStringWith:(NSString *)time

{

NSString *month;

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSString *nowTime = [formatter stringFromDate:[NSDate date]];

if ([[time substringToIndex:4] isEqualToString:[nowTime substringToIndex:4]])

{

if ([[time substringWithRange:NSMakeRange(5, 2)] isEqualToString:[nowTime substringWithRange:NSMakeRange(5, 2)]])

{

month = @"本月";

}

else

{

month = [NSString stringWithFormat:@"%@月", [time substringWithRange:NSMakeRange(5, 2)]];

}

}

else

{

month = @"更早";

}

return month;

}

+ (NSArray *)dataWithState:(NSString *)state andTime:(NSString *)time

{

NSMutableArray *dataArray = [NSMutableArray array];

return dataArray;

}

+ (BOOL)ifDateExistInArray:(NSArray *)array andTimeString:(NSString *)time

{

BOOL flag = NO;

//二维

for (NSArray *smallArray in array)

{

if ([smallArray isKindOfClass:[NSArray class]])

{

for (ALUserDetailAccount *model in smallArray)

{

if ([[model mid] isEqualToString:time])

{

return YES;

}

}

}

}

return flag;

}

+ (BOOL)ifDateExistInOneArray:(NSArray *)array andTimeString:(NSString *)time

{

for (ALUserDetailAccount *model in array)

{

if ([[model mid] isEqualToString:time])

{

return YES;

}

}

return NO;

}

+ (BOOL)iFDateIsTodayOrYesterdayWith:(NSString *)time

{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSString *nowTime = [formatter stringFromDate:[NSDate date]];

if ([[time substringToIndex:10] isEqualToString:[nowTime substringToIndex:10]])

{

return YES;

}

else

{

//是否是昨天

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSDate *inputDate = [formatter dateFromString:time];

NSDate *tomorrow = [NSDate dateWithTimeInterval:-1 * 60 * 60 * 24 sinceDate:inputDate];

NSString *tomorrowTime = [formatter stringFromDate:tomorrow];

NSString *temp = [tomorrowTime substringToIndex:10];

if ([temp isEqualToString:[nowTime substringToIndex:10]])

{

return YES;

}

else

{

return NO;

}

}

}

+ (BOOL)ifDateIsToday:(NSString *)time

{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSString *nowTime = [formatter stringFromDate:[NSDate date]];

if ([[time substringToIndex:10] isEqualToString:[nowTime substringToIndex:10]])

{

return YES;

}

return NO;

}

+ (NSString *)changeBirthdayStringToAge:(NSString *)time

{

NSString *ageStr;

if ([time length] < 10)

{

return @"0岁";

}

YRDateFormater *formatter = [[YRDateFormater alloc] init];

NSString *nowTime = [formatter dateToStringYMD:[NSDate date]];

NSString *birthday = [time substringToIndex:10];

NSInteger nowYear = [[nowTime substringToIndex:4] integerValue];

NSInteger nowMonth = [[nowTime substringWithRange:NSMakeRange(5, 2)] integerValue];

NSInteger nowDay = [[nowTime substringFromIndex:8] integerValue];

NSInteger birthdayYear = [[birthday substringToIndex:4] integerValue];

NSInteger birthdayMonth = [[birthday substringWithRange:NSMakeRange(5, 2)] integerValue];

NSInteger birthdayDay = [[birthday substringFromIndex:8] integerValue];

NSInteger age = nowYear - birthdayYear - 1;

if (nowMonth > birthdayMonth)

{

age++;

}

else if (nowMonth == birthdayMonth)

{

if (nowDay >= birthdayDay)

{

age++;

}

}

ageStr = [NSString stringWithFormat:@"%li岁 ", age];

return ageStr;

}

+ (BOOL)ifExerciseDataInArray:(NSArray *)array andMid:(NSString *)mid

{

if ([array count])

{

for (ALExerciseAllCellModel *obj in array)

{

if ([obj.mid isEqualToString:mid])

{

return YES;

}

}

}

return NO;

}

+ (BOOL)ifExercisePhotoIsExistInArray:(NSArray *)array andExerciseDate:(NSString *)date

{

if ([array count])

{

for (ALLMFExercisePhotosModel *model in array)

{

if ([model.exerciseTime isEqualToString:date])

{

return YES;

}

}

}

return NO;

}

+ (BOOL)ifTogetherExercisePhotoIsExistInArray:(NSArray *)array andExerciseDate:(NSString *)date

{

if ([array count])

{

for (ALLMFExerciseTogetherPhotosModel *model in array)

{

if ([model.exerciseTime isEqualToString:date])

{

return YES;

}

}

}

return NO;

}

+ (NSArray *)changeTimeToSeconds:(NSArray *)timeArray

{

NSMutableArray *secondsArray = [NSMutableArray array];

for (NSString *time in timeArray)

{

NSString *currentTime;

if ([time length] == 5)

{

currentTime = [NSString stringWithFormat:@"%@:00", time];

}

else

{

currentTime = [NSString stringWithFormat:@"0%@:00", time];

}

NSDateFormatter *judgeFormatter = [[NSDateFormatter alloc] init];

[judgeFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSString *nowTime = [[[judgeFormatter stringFromDate:[NSDate date]] substringFromIndex:11] stringByReplacingOccurrencesOfString:@" " withString:@""];

NSInteger currentHour = [[currentTime substringToIndex:2] integerValue];

NSInteger currentMinute = [[currentTime substringWithRange:NSMakeRange(3, 2)] integerValue];

NSInteger currenSecond = 0;

NSInteger nowHour = [[nowTime substringToIndex:2] integerValue];

NSInteger nowMinute = [[nowTime substringWithRange:NSMakeRange(3, 2)] integerValue];

NSInteger nowSecond = [[nowTime substringFromIndex:6] integerValue];

NSInteger timeCha;

NSInteger hourCha = currentHour - nowHour;

NSInteger minuteCha = currentMinute - nowMinute;

NSInteger secondCha = currenSecond - nowSecond;

timeCha = hourCha*3600+minuteCha*60+secondCha;

NSLog(@"%li", timeCha);

[secondsArray addObject:[NSString stringWithFormat:@"%li", timeCha]];

}

return secondsArray;

}

+ (BOOL)ifCardHasExist:(NSArray *)array and:(ALLMFTiXianListModel *)model

{

if ([array count])

{

for (int i=0; i<[array count]; i++)

{

ALLMFTiXianListModel *obj = [[array objectAtIndex:i] objectAtIndex:0];

if ([obj.cardNo isEqualToString:model.cardNo])

{

return YES;

}

}

return NO;

}

else

{

return NO;

}

}

+ (NSInteger)indexOfDateFromToday:(NSString *)time

{

NSInteger index;

/*获取当前日期*/

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSDate *date = [NSDate date];

/*将传入的时间拼接成标注格式的日期*/

NSString *currentTime = time;

if ([time length] < 19)

{

currentTime = [NSString stringWithFormat:@"%@ 00:00:00", time];

}

for (NSInteger i=0; i<10; i++)

{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSDate *tomorrow = [NSDate dateWithTimeInterval:-i * 60 * 60 * 24 sinceDate:date];

NSString *tomorrowTime = [formatter stringFromDate:tomorrow];

NSString *temp = [tomorrowTime substringToIndex:10];

index = i+1;

if ([temp isEqualToString:time])

{

return index;

}

}

return 11;

}

+ (BOOL)ifKnowledgeHasRead:(NSString *)mid

{

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

NSString *userId = [userDefaults objectForKey:@"userId"];

NSString *key = [NSString stringWithFormat:@"%@_hasRead", userId];

NSMutableArray *dicArray = [NSMutableArray array];

dicArray = [[[ALCacheService shareCacheService] getDictionaryForKey:key tableName:@"knowledgeTable"] objectForKey:@"hasRead"];

if ([dicArray count])

{

for (NSDictionary *dic in dicArray)

{

if ([mid isEqualToString:[dic objectForKey:@"hasRead"]])

{

return YES;

}

}

return NO;

}

else

{

return NO;

}

return NO;

}

+ (NSString *)changeDateToConstellation:(NSString *)time

{

NSString *constellation = @"猎户座";

NSInteger index = 0;

NSArray *constellationArray = @[@"魔蝎座", @"水瓶座" ,@"双鱼座" ,@"白羊座" ,@"金牛座" ,@"双子座" ,@"巨蟹座" ,@"狮子座", @"处女座", @"天秤座", @"天蝎座", @"射手座"];

/*12.22-1.19  1.20-2.18  2.19-3.20  3.21-4.19  4.20-5.20  5.21-6.21  6.22-7.22  7.23-8.22  8.23-9.22 9.23-10.23 10.24-11.22 11.23-12.21*/

NSDictionary *dic = @{@"12":@"22", @"1":@"20", @"2":@"19", @"3":@"21", @"4":@"20", @"5":@"21", @"6":@"22", @"7":@"23", @"8":@"23", @"9":@"23", @"10":@"24", @"11":@"23"};

/*

2016-10-10 00:00:00

规则是:首先将时间分割成月和日

通过月找到对应的日,判断日期是否大于临界点,大则加1

*/

NSString *month = @"3";

NSInteger day;

if ([time length] >= 10)

{

NSString *monthStr = [time substringWithRange:NSMakeRange(5, 2)];

month = [NSString stringWithFormat:@"%li", [monthStr integerValue]];

day = [[time substringWithRange:NSMakeRange(8, 2)] integerValue];

}

else if ([time length] == 4 || [time length] == 5)

{

NSRange range;

if ([time contains:@"-"])

{

range = [time rangeOfString:@"-"];

}

else if ([time contains:@"."])

{

range = [time rangeOfString:@"."];

}

else

{

range = NSMakeRange(0, 0);

}

if (range.length && range.location != NSNotFound)

{

NSString *monthStr = [time substringToIndex:range.location];

month = [NSString stringWithFormat:@"%li", [monthStr integerValue]];

day = [[time substringFromIndex:range.location+1] integerValue];

}

else

{

return constellation;

}

}

else

{

return constellation;

}

index = [month integerValue]%12;

NSInteger temp = [[dic objectForKey:month] integerValue];

if (day < temp)

{

index--;

if (index < 0)

{

index = 11;

}

}

return constellationArray[index];

}

+ (NSString *)changeDateToMonthAndDay:(NSString *)time

{

NSString *monthAndDay = @"01月01日";

if ([time length] >= 10)

{

NSString *month = [time substringWithRange:NSMakeRange(5, 2)];

NSString *day = [time substringWithRange:NSMakeRange(8, 2)];

monthAndDay = [NSString stringWithFormat:@"%@月%@日", month, day];

}

return monthAndDay;

}

@end

  上面有几个文件没有的可以删除,然后删除对应的几个方法。(菜鸟成长之路,望各位大神指教??)

时间: 2024-10-08 17:29:00

时间字符串以及时间戳解析的相关文章

用shell将时间字符串与时间戳互转

date的详细用户可以参考下面的 http://www.cnblogs.com/xd502djj/archive/2010/12/29/1919478.html date 的具体用法可以查看另外一篇博文 <shell date 命令详解>http://blog.csdn.net/runming918/article/details/7223520 date +%s   可以得到UNIX的时间戳;用shell将时间字符串与时间戳互转:      date -d "2010-10-18

JS 时间字符串与时间戳之间的转换

1.当前时间换时间戳 var timestamp = parseInt(new Date().getTime()/1000); // 当前时间戳 document.write(timestamp); 2.当前时间换日期字符串 var now = new Date(); var yy = now.getFullYear(); //年 var mm = now.getMonth() + 1; //月 var dd = now.getDate(); //日 var hh = now.getHours(

IOS 时间字符串转换时间戳失败问题

链接:https://pan.baidu.com/s/1nw6VWoD 密码:1peh 有时候获取到的时间带有毫秒数或者是(2018-2-6 11:11:11)格式的(别说你没遇到过,也别什么都让后台转好给你,程序员就是在长跑,短时间内看不出什么,但一年两年后,有的人成了大神,有的人却还是只会切图),这样的字符串在ie11和IOS系统上jquery的getTime()无法将其转为时间戳(谷歌,安卓(华为)可以). 本宝宝致力于高版本IE网站,和移动端H5网页小游戏(比如答题游戏,大转盘等等)开发

java 转化各种格式的时间字符串为时间戳

private static String[] parsePatterns = {"yyyy-MM-dd","yyyy年MM月dd日", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyyMMdd&q

js时间格式化工具,时间戳格式化,字符串转时间戳

在开发中经常会用到时间格式化,有时候在网上搜索一大堆但不是自己想要的,自己总结一下,写一个时间格式化工具方便以后直接使用,欢迎大家来吐槽-- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

JS时间戳与时间字符串之间的相互转换

时间字符串 转 时间戳 /** * 时间字符串 转 时间戳 * @param {String} time_str 时间字符串(格式"2014-07-10 10:21:12") * @returns {Number} 10位数的时间戳(秒值:1404958872000) */ const toTimestamp = time_str => +new Date(time_str) / 1000 默认转化后为Number类型后获得的是时间的毫秒数值,需求是要10位数的秒值,所以需要除以

技术杂记-日期时间字符串解析识别

日期时间的格式可谓千奇百怪,做数据汇集相关项目时就会需要识别各种日期时间格式(因为数据来源广泛不可控),然后转换为标准格式或Date类型. 我之前看到同事的一个时间解析方法,当时觉得已经很不错了,后来网上搜索发现这个方法在搜索结果中多次出现,来源就不清楚.我是因项目需要更精确的识别日期时间字符串,于是我慢慢改进原来的方法,以下就是改进后的方法,java语言实现,只要看懂逻辑,应该很容易翻译成其他语言. 1 /** 2 * 解析大部分常见日期格式 <br/> 3 * @param dateStr

Mysql 时间、字符串、时间戳互转

时间转字符串 select date_format(now(),'%Y-%m-%d'); //2018-04-12 时间转时间戳 select UNIX_TIMESTAMP(now()); //1523504912 时间戳转时间 select FROM_UNIXTIME(1523504912) //2018-04-12 11:48:32 时间戳转字符串 select FROM_UNIXTIME(1523504912,'%Y-%d') //2018-12 字符串转时间 select str_to_

python中时间戳,datetime 和时间字符串之间得转换

# datetime时间转为字符串def Changestr(datetime1):    str1 = datetime1.strftime('%Y-%m-%d %H:%M:%S')    return str1 # 字符串时间转为时间戳def Changetime(str1):    Unixtime = time.mktime(time.strptime(str1, '%Y-%m-%d %H:%M:%S'))    return Unixtime # datetime时间转为时间戳def