Monday 13 October 2014

Date & Time

Date and Time formats and conversions in R


#as.Date
#yyyy/mm/dd
Date1<-c("2013/07/15","2013/08/13","2013/07/23","2014/01/31","2013/02/13","2014/07/13")
as.Date(Date1,format="%Y/%m/%d",tz="Australia/Sydney")

[1] "2013-07-15" "2013-08-13" "2013-07-23" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#yy/mm/dd
Date2<-c("13/07/15","13/08/13","13/07/23","14/01/31","13/02/13","14/07/13")
as.Date(Date2,format="%y/%m/%d",tz="Australia/Sydney")

[1] "2013-07-15" "2013-08-13" "2013-07-23" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#yy/m/dd
Date3<-c("15/7/13","13/8/13","23/7/13","14/1/31","13/2/13","14/7/13")
as.Date(Date3,format="%y/%m/%d",tz="Australia/Sydney")

[1] "2015-07-13" "2013-08-13" "2023-07-13" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#dd mmm yyyy
Date4<-c("15 jul 2013","13 aug 2013","23 jul 2013","31 jan 2014","13 Feb 2013",
"13 Jul 2014")
as.Date(Date4,format="%d %b %Y",tz="Australia/Sydney")

[1] "2013-07-15" "2013-08-13" "2013-07-23" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#dd month yyyy
Date5<-c("15 july 2013","13 August 2013","23 july 2013","31 january 2014",
"13 February 2013","13 July 2014")
as.Date(Date5,format="%d %B %Y",tz="Australia/Sydney")

[1] "2013-07-15" "2013-08-13" "2013-07-23" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#weekday  abbreviated
DateProper<-as.Date(Date5,format="%d %B %Y",tz="Australia/Sydney")
format(DateProper,"%a")

[1] "Mon" "Tue" "Tue" "Fri" "Wed" "Sun"


#weekday full
format(DateProper,"%A")

[1] "Monday"    "Tuesday"   "Tuesday"   "Friday"    "Wednesday" "Sunday"


#weekdays alternative
weekdays(DateProper)

[1] "Monday"    "Tuesday"   "Tuesday"   "Friday"    "Wednesday" "Sunday"


#ISO C99 date format
format(DateProper,"%D")

[1] "07/15/13" "08/13/13" "07/23/13" "01/31/14" "02/13/13" "07/13/14"


#ISO 8601 date format
format(DateProper,"%F")

[1] "2013-07-15" "2013-08-13" "2013-07-23" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#day of month
format(DateProper,"%e")

[1] "15" "13" "23" "31" "13" "13"


#day of year
format(DateProper,"%j")

[1] "196" "225" "204" "031" "044" "194"


#week of year where week 1 is first full week staring Sunday
format(DateProper,"%U")

[1] "28" "32" "29" "04" "06" "28"


#week of year where week 1 is first week containing 4 or more days of the new year
format(DateProper,"%V")

[1] "29" "33" "30" "05" "07" "28"


#week of year where week 1 is first full week staring Monday
format(DateProper,"%W")

[1] "28" "32" "29" "04" "06" "27"


#strptime
#24 hour format
TimeStamp1<-c("2013/07/15 13:10:23","2013/08/13 13:10:23",
"2013/07/23 19:10:25","2014/01/31 03:10:23","2013/02/13 13:50:53","2014/07/13 21:13:43")

strptime(TimeStamp1,format="%Y/%m/%d %H:%M:%S",tz="Australia/Sydney")


[1] "2013-07-15 13:10:23 EST" "2013-08-13 13:10:23 EST"
[3] "2013-07-23 19:10:25 EST" "2014-01-31 03:10:23 EST"
[5] "2013-02-13 13:50:53 EST" "2014-07-13 21:13:43 EST"



#AM/PM format
TimeStamp2<-c("2013/07/15 1:10:23 PM","2013/08/13 1:10:23 PM",
"2013/07/23 7:10:25 PM","2014/01/31 3:10:23 AM","2013/02/13 1:50:53 PM",
"2014/07/13 9:13:43 PM")

TSProper<-strptime(TimeStamp2,
format="%Y/%m/%d %I:%M:%S %p",tz="Australia/Sydney")

TSProper

[1] "2013-07-15 13:10:23 EST" "2013-08-13 13:10:23 EST"
[3] "2013-07-23 19:10:25 EST" "2014-01-31 03:10:23 EST"
[5] "2013-02-13 13:50:53 EST" "2014-07-13 21:13:43 EST"

 
#fraction of seconds 
Use %OSn instead of %S. 
  
%OSn reads fractions of seconds i.e. decimal points. ‘n’ is a digit beteen 0 and 6 inclusive, indicating number of decimal places to display.
  
strptime("2018-10-31 15:33:33.677", "%Y-%m-%d %H:%M:%OS")
   
[1] "2018-10-31 15:33:33 AEDT"


#2 dec points
format(strptime("2018-10-31 15:33:33.677", "%Y-%m-%d %H:%M:%OS"), "%H:%M:%OS2")
  
[1] "15:33:33.67"
   

#3 dec points
format(strptime("2018-10-31 15:33:33.677","%Y-%m-%d %H:%M:%OS"),"%H:%M:%OS3")

   
[1] "15:33:33.676"



#hour & minute AM/PM
format(TSProper,"%r")

[1] "01:10:23 PM" "01:10:23 PM" "07:10:25 PM" "03:10:23 AM" "01:50:53 PM"
[6] "09:13:43 PM"


#hour & minute AM/PM without leading zeros
format(TSProper,"%X")

[1] "1:10:23 PM" "1:10:23 PM" "7:10:25 PM" "3:10:23 AM" "1:50:53 PM"
[6] "9:13:43 PM"


#hour & minute
format(TSProper,"%R")

[1] "13:10" "13:10" "19:10" "03:10" "13:50" "21:13"


#hour, minute & second
format(TSProper,"%T")

[1] "13:10:23" "13:10:23" "19:10:25" "03:10:23" "13:50:53" "21:13:43"


#full
format(TSProper,"%c")

[1] "Mon Jul 15 13:10:23 2013" "Tue Aug 13 13:10:23 2013"
[3] "Tue Jul 23 19:10:25 2013" "Fri Jan 31 03:10:23 2014"
[5] "Wed Feb 13 13:50:53 2013" "Sun Jul 13 21:13:43 2014"



#system time
Sys.time()
  
[1] "2015-04-20 13:26:46 AEST"


#system date
Sys.Date()
  
[1] "2015-04-20"


#time zones
The below function will list all time zones used.
OlsonNames()  

Example: 

OlsonNames()[1:10]   
  
 [1] "Africa/Abidjan"     "Africa/Accra"       "Africa/Addis_Ababa"
 [4] "Africa/Algiers"     "Africa/Asmara"      "Africa/Asmera"     
 [7] "Africa/Bamako"      "Africa/Bangui"      "Africa/Banjul"     
[10] "Africa/Bissau"   



#Converting times to another time zone use as.POSIXlt()

LocalTime <- Sys.time()  
  
#this is Australian Eastern Standard time (i.e. Sydney or Canberra)  
  
LocalTime   
  
[1] "2019-12-18 12:30:38 AEDT"   
  
  
#If we want to check what time it is in New York  
  
OlsonNames()[169]  
  
[1] "America/New_York"
  
  
NewYork <- as.POSIXlt(LocalTime, tz = OlsonNames()[169])
  
NewYork  
  
[1] "2019-12-17 20:30:38 EST"  
  
  
#To create a date time object use as.POSIXct()  
  
NewDateTime <- as.POSIXct("2018-01-01 05:00:00", "%Y-%m-%d %H:%M:%S", tz = "Europe/London")  
  
NewDateTime   
  
[1] "2018-01-01 05:00:00 GMT"  
   
  
  




No comments:

Post a Comment