转 http://stackoverflow.com/questions/1962278/dealing-with-timestamps-in-r

You want the (standard) POSIXt type from base R that can be had in 'compact form' as a POSIXct (which is essentially a double representing fractional seconds since the epoch) or as long form in POSIXlt (which contains sub-elements). The cool thing is that arithmetic etc are defined on this -- see help(DateTimeClasses)

Quick example:

R> now <- Sys.time()
R> now
[] "2009-12-25 18:39:11 CST"
R> as.numeric(now)
[] 1.262e+09
R> now + # adds seconds
[] "2009-12-25 18:39:21 CST"
R> as.POSIXlt(now)
[] "2009-12-25 18:39:11 CST"
R> str(as.POSIXlt(now))
POSIXlt[:], format: "2009-12-25 18:39:11"
R> unclass(as.POSIXlt(now))
$sec
[] 11.79 $min
[] $hour
[] $mday
[] $mon
[] $year
[] $wday
[] $yday
[] $isdst
[] attr(,"tzone")
[] "America/Chicago" "CST" "CDT"

As for reading them in, see help(strptime)

As for difference, easy too:

R> Jan1 <- strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
R> difftime(now, Jan1, unit="week")
Time difference of 51.25 weeks

Lastly, the zoo package is an extremely versatile and well-documented container for matrix with associated date/time indices.

timestamp to datestr:

datestr = as.character(as.POSIXlt(timestamp, origin="1970-01-01"))

R 时间戳转化的更多相关文章

  1. excel时间戳转化为标准日期(日期转化为日期戳)

    最近在学习python将数据导入到excel,发现日期变成数字而不是日期格式的问题. 第一眼看去肯定是excel单元格格式问题,一般excel单元格格式为常规,而常规处理日期时就显示为数字,所以就想到 ...

  2. js时间戳转化时间格式

    // 判断是否前面补0 add0 (m) { return m < 10 ? '0' + m : m }, // 时间转化 timeFormat (timestamp) { // timesta ...

  3. vue filters 时间戳转化成时间格式

    vue filters 时间戳转化成时间格式 filters: { formatDate: function (time) { var re = /-?\d+/ var m = re.exec(tim ...

  4. JS将时间戳转化为时间

    //将时间戳转化为时间 function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);//时间戳为10位需*1 ...

  5. jQuery将时间转化为时间戳或将时间戳转化为时间

    下面的这段代码,是可以将时间戳转为时间,或者将时间戳转为时间: <script type="text/javascript"> $.extend({ myTime:{ ...

  6. 囧啊!!时间戳转化为时间出错php

    最近写了一个api,测试也没发现啥问题.可是上线之后发现有时api的返回结果不正确.为什么呢? 调我接口的同学给了两个调用示例,理论上两个的结果应该一致,实际结果却不一致. api调用带了一个时间戳参 ...

  7. 【微信小程序】处理时间格式,时间戳转化展示时间格式问题,调用外部js的默认方法function的问题

    默认的 小程序中new Date()显示的时间是这样的: 格式化时间的显示怎么做: 小程序的根目录下util目录下默认有一个util.js文件 其中util.js文件内容是: //数据转化 funct ...

  8. 时间戳转化为时间&&截取时间的年月日

    时间戳转化为正常的时间格式 function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { // 在这里 date.getFullYear() + ' ...

  9. JavaScript获取时间戳与时间戳转化

    第一种方法(精确到秒): var timestamp1 = Date.parse( new Date()); 第二种方法(精确到毫秒): var timestamp2 = ( new Date()). ...

随机推荐

  1. [LeetCode] 213. 打家劫舍 II

    题目链接:https://leetcode-cn.com/problems/house-robber-ii/ 题目描述: 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有 ...

  2. @Transactional实现原理

    Transactional是spring中定义的事务注解,在方法或类上加该注解开启事务.主要是通过反射获取bean的注解信息,利用AOP对编程式事务进行封装实现.AOP对事务的封装可以看我的这篇文章的 ...

  3. 2019 湖湘杯 Reverse WP

    0x01 arguement 下载链接:https://www.lanzous.com/i7atyhc 1.准备 获取到信息: 32位的文件 upx加密文件 在控制台打开文件 使用"upx ...

  4. git分支/标签操作

    git分支类似于某一个模块,等到所有模块开发完毕时,最后聚合在一起形成一个项目.而分支之间一般是不会受影响的. git 分支的基本操作: git branch branchname 表示创建分支,新建 ...

  5. Vue框架前言

    Vue框架 Vue 框架: 官网 vue框架:渐进式JavaScript框架 vue一个环境:可以只控制页面中一个标签.可以控制一组标签.可以控制整个页面.可以控制整个项目 vue可以根据实际需求,选 ...

  6. python email模块

    python email模块 官方文档 email模块 电子邮件包是一个用于管理电子邮件消息的库.它的特殊设计不用于向SMTP (RFC 2821).NNTP或其他服务器发送任何电子邮件消息;这些是模 ...

  7. nsswitch.conf - 系统数据库及名字服务开关配置文件

    DESCRIPTION C 程序库里很多函数都需要配置以便能在本地环境正常工作, 习惯上是使用文件(例如`/etc/passwd') 来完成这一任务. 但别的名字服务, 如网络信息服务NIS, 还有域 ...

  8. mysql5.7.26-忘记密码(CentOS为例)

    编辑 my.cnf [mysqld]下插入 skip-grant-tables [root@VM_0_7_centos mysql]# vim /etc/my.cnf # For advice on ...

  9. bzoj4238 & loj2881 电压 二分图判定+dfs树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4238 https://loj.ac/problem/2881 题解 如果想要让每一条边都有电流 ...

  10. springboot 集成p6spy

    pom.xml <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifact ...