时间与时间戳的转换

?





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

时间戳<span>是一种时间表示方式,定义为从</span><a href="http://baike.baidu.com/view/856.htm"
target="_blank">格林威治时间</a><span>1970年01月01日00时00分00秒起至现在的总秒数</span><br>//现在时间

    NSDate
*nowTime = [NSDate
date];

    //获取时区

    NSTimeZone
*zone = [NSTimeZone
systemTimeZone];

    NSInteger
interVal = [zone secondsFromGMTForDate:nowTime];

    NSDate
*localTime = [nowTime dateByAddingTimeInterval:interVal];//本地时间

    //时间戳转为时间

    NSString
*time = @"1400386922";

    NSInteger
dTime = [time integerValue];

    NSDate
*publishTime = [NSDate
dateWithTimeIntervalSince1970:dTime];

    NSLog(@"%@", publishTime);

    

    //计算时间间隔(localTime - publishTime)

    NSTimeInterval
timeInterval = [localTime timeIntervalSinceDate:publishTime];

    NSLog(@"%f", timeInterval);

    if
(timeInterval < 60) {

       

        NSString
*time = @"刚刚";

         NSLog(@"刚刚!!");

    }

    if
(timeInterval >= 60 && timeInterval < 3600) {

        int
a = timeInterval / 60;

        NSString
*time = [NSString
stringWithFormat:@"%d分钟前", a];

        NSLog(@"%@", time);

    }

    if
(timeInterval >= 3600 && timeInterval < 3600 * 24) {

        int
a = timeInterval / 3600;

       

        NSString
*time = [NSString
stringWithFormat:@"%d小时前", a];

        NSLog(@"%@", time);

    }

    if
(timeInterval >= 3600 * 24 && timeInterval < 3600 * 24 * 31) {

        int
a = timeInterval / (3600 * 24);

        NSString
*time = [NSString
stringWithFormat:@"%d天前", a];

       NSLog(@"%@", time);

    }

  

时间: 2024-07-31 04:41:17

时间与时间戳的转换的相关文章

格式化时间 与 时间戳的转换

2015-08-06 19:37:58 1 /*! 2 * <格式化时间与时间戳的转换> 3 * 4 * 2015/08/06 by <felove> 5 */ 6 #include <stdio.h> 7 #include <windows.h> 8 #include <time.h> 9 10 void timestampToFormat(time_t& t_1, tm& tm_1); 11 time_t formatToTi

js 时间与时间戳的转换

一:时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳 1.var timestamp1 = Date.parse(new Date()); // 结果:1477808630000 不推荐这种办法,毫秒级别的数值被转化为000 console.log(timestamp1); 2.var timestamp2 = (new Date()).valueOf(); // 结果:1477808630404 通过valueOf

java,时间转时间戳的转换以及各种date格式的转化

package com.henry.test; import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map; import org.apache.commons.lang.StringUtils; import com.shopin.core.util.HttpClientUtil; public class test { /** * 说明: * @pa

Shell日期时间和时间戳的转换

Gitlab的备份文件是以时间戳显示的,类似:1438624820_gitlab_backup.tar 为了更易于阅读,想把文件名转换成日期格式:2015-08-04_gitlab_backup.tar 所以查找了下Shell里时间转换的方法,记录如下备忘. Linux下时间转换的一些命令: date +%s   可以得到UNIX的时间戳; 用shell将日期时间与时间戳互转:       date -d "2015-08-04 00:00:00" +%s     输出:1438617

本区区时当前时间与时间戳的转换

/// <summary> /// 本区区时当前时间转换为时间戳 /// </summary> /// <param name="localTime">本区区时当前时间</param> /// <returns>时间戳</returns> public static long ConvertLocalTimeToTimestamp(DateTime localTime) { DateTime greenwishTi

mysql 将时间戳直接转换成日期时间

date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串 后面的 '%Y%m%d' 主要是将返回值格式化 例如: mysql>SELECT FROM_UNIXTIME( 1249488000, '%Y%m%d' ) ->20071120 mysql>SELECT FROM_UNIXTIME( 1249488000, '%Y年%m月%d' ) ->2007年11月20 UNIX_TIMESTAMP()是与之相对正好相反的时间函数 UNIX

python——时间与时间戳之间的转换

1.将时间转换成时间戳 将如上的时间2017-09-16 11:28:54转换成时间戳 利用strptime()函数将时间转换成时间数组 利用mktime()函数将时间数组转换成时间戳 #!/usr/bin/env python # -*- coding:utf-8 -*- import time dtime= "2017-09-16 11:28:54" #转换成时间数组 timeArray = time.strptime( dtime, "%Y-%m-%d %H:%M:%S

[转]时间与时间戳之间的转换

文章来源:http://blog.csdn.net/google19890102/article/details/51355282 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换为时间戳 重新格式化时间 时间戳转换为时间 获取当前时间及将其转换成时间戳 1.将时间转换成时间戳 将如上的时间2016-05-05 20:28:54转换成时间戳,具体

好长时间没来这里了,今天复习一下java的东西,关于Date类和时间戳的转换

在开发过程中经常会遇到对时间类的使用,而说到Date类则时间戳就不得不提一下,关于时间戳的定义大家可以看看网上对时间戳的定义,我今天只介绍一下Date类和时间戳直接的转换:下面我把自己做的两个小Demo贴出来也许对刚接触java的朋友会有帮助,也是我学习过程中的一点总结,如果有什么不足之处希望各位大神多多指教!! import java.util.*; import java.text.SimpleDateFormat; import java.text.ParseException; publ