-
根据年月日时间换算到精确时分秒和周几
2016-03-10 17:25:00现在我根据后台返回的时间年月日时分秒来转换时间demo地址:https://github.com/tuwanli/ShowTimeDemo 后台请求的数据: 该建表的建表,该创建cell的创建cell,然后就是将数据显示在表里 主要转换的代码如下 + ...闲来无事看到微信列表右侧有显示时间,可以清晰的看出来具体什么时间收到的消息
现在我根据后台返回的时间年月日时分秒来转换时间demo地址:https://github.com/tuwanli/ShowTimeDemo
demo:http://download.csdn.net/detail/tuwanli125/9470326
后台请求的数据:
该建表的建表,该创建cell的创建cell,然后就是将数据显示在表里
主要转换的代码如下
+ (NSString*)weekDayStr:(NSDate *)format { NSString *weekDayStr = nil; NSDateComponents *comps = [[NSDateComponents alloc] init]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSInteger unitFlags =NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitSecond; comps = [calendar components:unitFlags fromDate:format]; NSString *str = [self description]; if (str.length >= 10) { NSString *nowString = [str substringToIndex:10]; NSArray *array = [nowString componentsSeparatedByString:@"-"]; if (array.count == 0) { array = [nowString componentsSeparatedByString:@"/"]; } if (array.count >= 3) { NSInteger year = [[array objectAtIndex:0] integerValue]; NSInteger month = [[array objectAtIndex:1] integerValue]; NSInteger day = [[array objectAtIndex:2] integerValue]; [comps setYear:year]; [comps setMonth:month]; [comps setDay:day]; } } NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDate *_date = [gregorian dateFromComponents:comps]; NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitWeekday fromDate:_date]; NSInteger week = [weekdayComponents weekday]; week ++; switch (week) { case 1: weekDayStr = @"星期日"; break; case 2: weekDayStr = @"星期一"; break; case 3: weekDayStr = @"星期二"; break; case 4: weekDayStr = @"星期三"; break; case 5: weekDayStr = @"星期四"; break; case 6: weekDayStr = @"星期五"; break; case 7: weekDayStr = @"星期六"; break; default: weekDayStr = @""; break; } return weekDayStr; } + (NSString *)remindlistTime:(NSString *)timeStr { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSDate* date = [formatter dateFromString:timeStr]; NSTimeInterval secondsPerDay = 24 * 60 * 60; NSDate *today = [[NSDate alloc] init]; NSDate *yesterday,*weekday; weekday = [today dateByAddingTimeInterval: -(secondsPerDay*7)]; yesterday = [today dateByAddingTimeInterval: -secondsPerDay]; // 10 first characters of description is the calendar date: NSString * todayString = [[today description] substringToIndex:10]; NSString * yesterdayString = [[yesterday description] substringToIndex:10]; NSString * dateString = [[date description] substringToIndex:10]; if ([dateString isEqualToString:todayString]) { return @"今天"; } else if ([dateString isEqualToString:yesterdayString]) { return @"昨天"; }else if ([weekday timeIntervalSince1970]>[date timeIntervalSince1970]){ NSArray *timeArr = [[[timeStr componentsSeparatedByString:@" "] objectAtIndex:0] componentsSeparatedByString:@"-"]; NSString *dayTime; NSString *monthlyTime; if ([[timeArr[2] substringToIndex:1] isEqualToString:@"0"]) { dayTime = [timeArr[2] substringFromIndex:1]; }else{ dayTime = timeArr[2]; } if ([[timeArr[1] substringToIndex:1] isEqualToString:@"0"]) { monthlyTime = [timeArr[1] substringFromIndex:1]; }else{ monthlyTime = timeArr[1]; } return [NSString stringWithFormat:@"%@/%@/%@",[(NSString *)timeArr[0] substringFromIndex:2],monthlyTime,dayTime]; } else { return [Tool weekDayStr:date]; } }
效果:如果你发现你看不见我单元格的线,告诉你一个秘密,因为我用的iOS9的模拟器,像素太高,所以看不见,command+1就能看见了,哈哈
-
JS将秒数换算成时分秒 以及转化为年月日 时分秒以及多长时间以前
2019-01-22 09:45:29将时间搓转化为 yyyy-mm-dd hh:MM:ss这种格式的 formatDateTime: function(inputTime) { var date = new Date(inputTime); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m ('0' + ...function formatDate(value) {
var secondTime = parseInt(value);// 秒
var minuteTime = 0;// 分
var hourTime = 0;// 时
if(secondTime > 60) {//如果秒数大于60,将秒数转换成整数
//获取分钟,除以60取整数,得到整数分钟
minuteTime = parseInt(secondTime / 60);
//获取秒数,秒数取佘,得到整数秒数
secondTime = parseInt(secondTime % 60);
//如果分钟大于60,将分钟转换成小时
if(minuteTime > 60) {
//获取小时,获取分钟除以60,得到整数小时
hourTime = parseInt(minuteTime / 60);
//获取小时后取佘的分,获取分钟除以60取佘的分
minuteTime = parseInt(minuteTime % 60);
}
}
var result = "" + parseInt(secondTime) + "秒";
if(minuteTime > 0) {
result = "" + parseInt(minuteTime) + "分" + result;
}
if(hourTime > 0) {
result = "" + parseInt(hourTime) + "小时" + result;
}
return result;
}
console.log(formatSeconds(10000002))将时间搓转化为 yyyy-mm-dd hh:MM:ss这种格式的
formatDateTime: function(inputTime) {
var date = new Date(inputTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y+'-'+m+'-'+d+' '+' '+h+':'+minute+':'+second;
}格式化时间为多久以前
function ReloadPubdate(string) {
var re = /^(\d{2,4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
if (re.test(string)) {
var t = string.match(re);
var d = new Date(t[1], t[2] - 1, t[3], t[4], t[5], t[6]);
var c = new Date();
var s = (c.getTime() - d.getTime()) / 1000;
var m = Math.floor(s / 60);
var h = Math.floor(s / 3600);
var d = Math.floor(s / 86400);
var n = Math.floor(s / (86400 * 30));
var y = Math.floor(s / (86400 * 365));
if (y > 0) return y + "年以前";
if (n > 0) return n + "个月以前";
if (d > 0) return d + "天以前";
if (h > 0) return h + "小时以前";
if (m > 0) return m + "分钟以前";
}
return "刚刚";
}想要整理更多的碎片知识,扫码关注下面的公众号,让我们在哪里接着唠!
-
Vue时间换算工具
2021-03-08 17:54:53Vue时间换算工具 format-time.js /* * 转换日期 转换格式可按照年月日时分秒配置 YYYY-MM-DD HH:mm:ss * formatDate('2019.11.11 11:11:20', 'YYYY-MM-DD HH:mm:ss'); //"2019-11-11 11:11:20" * formatDate('...Vue时间换算工具 format-time.js
/* * 转换日期 转换格式可按照年月日时分秒配置 YYYY-MM-DD HH:mm:ss * formatDate('2019.11.11 11:11:20', 'YYYY-MM-DD HH:mm:ss'); //"2019-11-11 11:11:20" * formatDate('2019-11-11'); // 2019-11-11 */ const formatDate = (date, template = 'YYYY-MM-DD', blankValue = '-') => { if (!date) { return blankValue; } const specs = 'YYYY:MM:DD:HH:mm:ss'.split(':'); // eslint-disable-next-line no-mixed-operators const timeStamp = new Date(date).getTime() - new Date().getTimezoneOffset() * 6e4; const res = new Date(timeStamp); return res .toISOString() .split(/[-:.TZ]/) .reduce((acc, item, i) => acc.split(specs[i]).join(item), template); }; const formatTime = (date, blankValue) => formatDate(date, 'YYYY-MM-DD HH:mm:ss', blankValue); const formatDiagonal = (date, blankValue = '/') => formatDate(date, 'YYYY/MM/DD', blankValue); const formatYmTime = (date, blankValue = '-') => { if (!date) { return blankValue; } const res = new Date(date); const Y = `${res.getFullYear()}年`; const M = `${res.getMonth() + 1}月`; return Y + M; }; const formatToM = (date, blankValue) => formatDate(date, 'YYYY-MM-DD HH:mm', blankValue); const formatHMS = (date, blankValue) => formatTime(date, blankValue).slice(-8); const formatYm = (date, blankValue) => formatYmTime(date, blankValue); const showtime = (time) => { const date = typeof time === 'number' ? new Date(time) : new Date((time || '').replace(/-/g, '/')); const diff = (new Date().getTime() - date.getTime()) / 1000; const dayDiff = Math.floor(diff / 86400); const isValidDate = Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime()); if (!isValidDate) { window.console.error('不是有效日期格式'); } const tempFormatDate = (temDate) => { const today = new Date(temDate); const year = today.getFullYear(); const month = (`0${today.getMonth() + 1}`).slice(-2); const day = (`0${today.getDate()}`).slice(-2); const hour = today.getHours(); const minute = today.getMinutes(); const second = today.getSeconds(); return `${year}-${month}-${day} ${hour}:${minute}:${second}`; }; // 小于0或者大于等于31显示原时间 if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 31) { return tempFormatDate(date); } return ( (dayDiff === 0 && ((diff < 60 && '刚刚') || (diff < 120 && '1分钟前') || (diff < 3600 && `${Math.floor(diff / 60)}分钟前`) || (diff < 7200 && '1小时前') || (diff < 86400 && `${Math.floor(diff / 3600)}小时前`))) || (dayDiff === 1 && '昨天') || (dayDiff < 7 && `${dayDiff}天前`) || (dayDiff < 31 && `${Math.ceil(dayDiff / 7)}周前`) ); }; export default { formatDate, formatTime, formatToM, formatHMS, formatYm, formatDiagonal, showtime };
使用方法
在 main.js 里引入
在自己的页面就可以使用了
-
python 年月日时分秒的格式数据怎么换算成秒_使用python获取视频时长/并将秒转化为时分秒的格式...
2020-12-10 18:18:35获取当前py文件同级目录以及子目录下的所有视频...废话少数说,上码:# -*- coding: utf-8 -*-def time_convert(seconds):"""将秒换成合适的时间,如果超过一分钟就换算成"分钟:秒",如果是小时,就换算成"小时:分钟:...获取当前py文件同级目录以及子目录下的所有视频的时长(此处代码写的是mp4,其他格式请将mp4改为其他格式后自测),把下面这长代码保存为py后,放到你的视频的目录下执行即可输出视频的时长。
废话少数说,上码:
# -*- coding: utf-8 -*-
def time_convert(seconds):
"""
将秒换成合适的时间,如果超过一分钟就换算成"分钟:秒",如果是小时,就换算成"小时:分钟:秒"单位换算
"""
print(f'时间换算{seconds}')
M,H = 60,3600
if seconds < M:
return f'00:00:0{seconds}' if seconds < 10 else f'00:00:{str(seconds)}'
elif seconds < H:
_M = int(seconds/M)
_S = int(seconds%M)
return f'00:{f"0{_M}" if _M < 10 else str(_M)}:{f"0{_S}" if _S < 10 else str(_S)}'
else:
_H = int(seconds/H)
_M = int(seconds%H/M)
_S = int(seconds%H%M)
return f'{f"0{_H}" if _H < 10 else str(_H)}:{f"0{_M}" if _M < 10 else str(_M)}:{f"0{_S}" if _S < 10 else str(_S)}'
def get_video_times(video_path):
"""
pip install moviepy
获取指定的视频时长
"""
from moviepy.editor import VideoFileClip
video_clip = VideoFileClip(video_path)
durantion = video_clip.duration
video_clip.reader.close()
video_clip.audio.reader.close_proc()
return durantion
def get_current_path():
"""
获取当前文件所在的目录/获取当前路径
"""
import os
return os.path.abspath(os.path.dirname(__file__)) # 获取当前py文件所在文件夹的路径
def get_video_duration(root_path):
"""
获取指定目录下以及目录下的所有子目录内的mp4文件的视频时长
"""
import os
for root, dirs, files in os.walk(root_path):
# 递归遍历当前py目录下的所有目录及文件,比如遍历到/a/aa/aaa/aaa.txt,则root='/a/aa/aaa/',dir=root路径下的所有文件夹名称,dir=root路径下的所有文件的名称
# print(root) # 当前所在路径
# print(dirs) # 当前所在路径下的所有目录名
# print(files) # 当前所在路径下的所有文件名
for file_name in files:
if file_name.endswith('.mp4'):
duration = time_convert(get_video_times(os.path.join(root,file_name)))
print(f'{file_name} - {duration}')
def run():
import os
path = os.path.abspath(os.path.dirname(__file__)) # 获取当前py文件所在目录的路径
get_video_duration(path)
if __name__ == "__main__":
run()
效果图:
其中最主要代码是获取视频时长的这一段:
def get_video_times(video_path):
"""
pip install moviepy
获取指定的视频时长,单位是秒
"""
from moviepy.editor import VideoFileClip
video_clip = VideoFileClip(video_path)
durantion = video_clip.duration
video_clip.reader.close()
video_clip.audio.reader.close_proc()
return durantion
然后再根据获取到的秒数转换成HH:mm:ss:
def time_convert(seconds):
"""
将秒换成合适的时间,如果超过一分钟就换算成"分钟:秒",如果是小时,就换算成"小时:分钟:秒"单位换算
"""
print(f'时间换算{seconds}')
M,H = 60,3600
if seconds < M:
return f'00:00:0{seconds}' if seconds < 10 else f'00:00:{str(seconds)}'
elif seconds < H:
_M = int(seconds/M)
_S = int(seconds%M)
return f'00:{f"0{_M}" if _M < 10 else str(_M)}:{f"0{_S}" if _S < 10 else str(_S)}'
else:
_H = int(seconds/H)
_M = int(seconds%H/M)
_S = int(seconds%H%M)
return f'{f"0{_H}" if _H < 10 else str(_H)}:{f"0{_M}" if _M < 10 else str(_M)}:{f"0{_S}" if _S < 10 else str(_S)}'
-
js有关时间换算的一些方法
2018-06-01 15:48:40工作中常常会遇到后台返回的值是毫秒,这时候就...1、换算年月日 function timeFormat(date) { var format = 'yyyy-MM-dd'; var t = new Date(date); var tf = function(i) { return (i < 10 ? '0'... -
js将中国标准时间转化为年月日时分秒(yyyy-mm-dd)格式以及时间戳,日期,天数之间的转换
2020-05-23 10:43:24近期在写后台管理系统的项目,时间戳,中国标准时间以及日期换算成天数。用到的频率比较高 中国标准时间(Thu May 12 2016 08:00:00 GMT+0800 )转化为yyyy-MM-dd格式 例如:var chinaStandard= 'Thu May 12 2020... -
传开始和结束格式时间,换算时间差
2018-02-07 14:33:00通过计算两个时间点,获取差距的年月日,时分秒 function getDifference($start,$end) { // $start = strtotime($start); // $end = strtotime($end); if ($start > $end){ $diff_time = $start-$end... -
python 时间戳 日期 字符串相互转换, 及其日期相关换算
2020-07-27 10:19:54import datetime import time import pandas import calendar from dateutil.parser import parse from dateutil....2. 获取年月日 (2020, 4, 6) 3. 字符串转换时间 2020-04-06 type <class 'datetime.dat -
linux java时间_Linux时间转化方法
2021-03-03 16:08:1927:2运维Linux时间转化方法:(1)date -d"2008年 12月 17日 星期三 17:27:22 CST" +"%s"该命令将2008年 12月 17日 星期三 17:27:22 CST转化为时间戳结果:1229515680(2)将时间戳1123495443 换算成可以识别的年月日分秒... -
linux时间(二 设置系统时间)
2010-08-31 16:28:00/******************* 设置系统时间 ********************/ ... 所以需要将年月日的时间换算成从1900年到现在的秒数。下面这个函数做这个工作。 time_t mktime(struct tm * t -
Linux时间转化方法
2011-09-23 12:01:00Linux时间转化方法: (1)date -d"2008年 12月 17日 星期三 17:27:22 CST" +"... (2)将时间戳1123495443 换算成可以识别的年月日分秒 date -d '1970-01-01 UTC 1123495443 second... -
C/C++获取当前系统时间的方法
2018-06-04 15:13:101、使用系统函数,并且可以修改系统时间#include...}备注:获取的为 小时:分钟:秒 信息2、获取系统时间(秒级),可以换算为年月日星期时分秒#include<iostream> #include<time.h> us... -
Linux/Freebsd下时间转化
2017-11-14 22:35:00Linux下: (1)date -d"2008年 12月 17日 星期三 17:27:22 CST" +"%s" 该命令将2008年 12月 17日 星期三 17:27:22 CST转化为时间戳...(2)将时间戳1123495443 换算成可以识别的年月日分秒 date -d '1970-01-01 UTC 1... -
时间与太阳、月亮和地球的关系
2020-03-24 12:27:35GoldTime 工具是我大概20年前写的,主要用于对时间进行换算,解决GPS周、秒与我们习惯的年月日的对应问题,那是刚开始从事GPS定位算法的研究,正做硕士论文,开始学习C++,学习面向对象,程序猿一点点的开始成长了…... -
写SQL就必须【收藏】的时间函数汇总!超详细!
2020-06-05 13:09:33怎么计算两个‘年月日时分秒’时间戳的时间间隔呢? SELECT to_unixtime(cast(a.apply_time as timestamp)) - to_unixtime(cast(b.su_time as timestamp)) as intervel 即可获得两个时间戳的时间间隔,单位为秒,/60... -
js实现文章或个人动态发布了多久的时间描述:几分钟前,几小时前,几天前等
2018-08-15 12:02:16一般来说,为提高用户体验,在某些管理文章或个人动态时,需要...2.要明确年月日时分秒的获取和相互之间的单位换算; 3.怎么用逻辑去判断,去返回详细的时间描述。 理解上面三个步骤之后,我们来实现一下简单的需... -
用date计算从出生到现在的天数
2020-01-02 20:23:55需求 输入自己的出生年月 从系统中拿出当前月 计算出生到现在的天数 思路 利用date内的方法编写出...将时间转为毫秒(从1970年1月1日开始计算) 两个日期相减 用毫秒换算为天数 代码 总结 又是收获满满的一天,加油! ... -
tinydate.js[v0.1]关于Javascript Date的工具
2018-06-14 17:55:00编写初衷 JavaScript的Date对象所提供的方法无法满足生产的需要,比如他的Month,他的日期加减,比如我要得到1993年1月1日到现在有多少天,比如我想...直接获取某个时间戳、时间对象、时间字符串的年月日时分秒毫秒... -
DateTime.Now.Ticks.ToString()是什么意思 Ticks
2014-06-27 11:21:46一个以0.1纳秒为单位的时间戳, 就是一个long型的数, 其实DateTime本质上就是一个long型的,通过0.1纳秒的单位,换算成各种时间,如果分,秒,年月日等等这些组合起来就是一个DateTime类型了 -
datetime.now.ticks java_DateTime.Now.Ticks.ToString("x")
2021-03-09 05:15:321、DateTime.Now.Ticks.ToString()一个以0.1纳秒为单位的时间戳,就是一个long型的数,其实DateTime本质上就是一个long型的,通过0.1纳秒的单位,换算成各种时间,如果分,秒,年月日等等这些组合起来就是一个... -
模拟机械时钟
2017-03-13 20:11:00此外new一个Date(),我们还可以利用它的许多方法获取到具体的年月日时分秒,甚至是时间累计的字符串,时间戳是距离1970年1月1日换算。 那么我们如何利用这点模拟机械时钟的转动呢? 首先,利用html5的canvas画... -
DateTime.Now.Ticks.ToString()说明
2014-12-01 22:34:57一个以0.1纳秒为单位的时间戳,就是一个long型的数,其实DateTime本质上就是一个long型的,通过0.1纳秒的单位,换算成各种时间,如果分,秒,年月日等等这些组合起来就是一个DateTime类型了举例:计算两个时间相隔... -
localtime函数在不同平台使用注意
2018-12-17 23:56:281.写在前面 localtime函数是C语言标准库中时间库...而在一些嵌入式场合,我们也需用到将时间戳转换成“年月日时分秒”格式,如不额外自行编写换算函数,可以直接调用该函数。在以往开发非联网或者国内使用... -
DateTime.Now.Ticks.ToString("x")
2017-08-23 10:32:00一个以0.1纳秒为单位的时间戳,就是一个long型的数,其实DateTime本质上就是一个long型的,通过0.1纳秒的单位,换算成各种时间,如果分,秒,年月日等等这些组合起来就是一个DateTime类型了 举例:计算两个时间相隔... -
Scratch积木式编程第一课-时钟
2019-12-17 14:48:45今日我们来利用scratch制作一个时钟,这款时钟和真正的时钟没有什么不同,除了具有时针、分针、秒针之外,还具备电子显示时分秒的功能,同时还能显示上午/下午,以及当日的年月日信息,那么这样儿一款时钟是如何制作... -
请问DateTime.Now.Ticks.ToString()是什么意思
2009-08-31 22:38:00一个以0.1纳秒为单位的时间戳,就是一个long型的数,其实DateTime本质上就是一个long型的,通过0.1纳秒的单位,换算成各种时间,如果分,秒,年月日等等这些组合起来就是一个DateTime类型了 =======================... -
Excel百宝箱 9.0 破解版 批量导入图片等200种功能
2013-05-11 22:46:24【修改文件时间】随心所欲修改文件的创建时间,包括年月日时分秒 【按颜色汇总】按背景色对选区的数据合类合计 【反向选择】选择当前区域中未选择的区域 【保护公式】保护当前工作表所有公式,不让人看到公式本身,...