Date printed in wrong format

Asked

Viewed 63 times

1

I am developing an app in Shiny and I have a date printing problem, both in the browser and in the . csv file generated. She gets that shape:

inserir a descrição da imagem aqui

The code for obtaining the date is

baseEstatistica[6,2] <- Sys.Date()

Other dates generated differently are being printed the same way!

3 answers

2

The answer was simpler than I imagined, it was enough to use a

baseEstatistica[6,2] <- as.character(Sys.Date())

But the date came out in the format below:

Formato da data no código

I updated the date code to the format below:

datavaalicao <- as.character(dataMesAvaliacao)
baseEstatistica[6,2] <- paste(str_sub(datavaalicao, 9,10),str_sub(datavaalicao, 6,7),str_sub(datavaalicao, 1,4), sep = "/")

2


You can do everything in one line of code. And much more readable. There is a method format.Date for class objects "Date", just use it.
It also has the advantage of being able to use any date format, a complete list is here or see help("strptime").

format(Sys.Date(), format = "%d/%m/%Y")
#[1] "06/01/2020"

baseEstatistica[6,2] <- format(Sys.Date(), format = "%d/%m/%Y")
  • Much better, I marked it as an answer.

0

There are several forms of date formatting, following some examples and their formations: Formatting Dates

%d: dia do mês em 2 dígitos (13)
%m: mês em 2 digitos (01)
%y: ano em 2 dígitos (82)
%Y: ano em 4 dígitos (1982)
%A: dia da semana (Friday)
%a: dia da semana abreviado (Fri)
%B: mês (July)
%b: mês abreviado (Jul)

Formatting Time

%H: hora (00-23)
%M: minuto
%S: segundo
%T: formado reduzido para %H:%M:%S

Formatting the output - as. Date()

as.Date("2016-06-28")
as.Date("20160628434")
as.Date("Jun-28-16", format = "%b-%d-%y")
as.Date("28 June, 2016", format = "%d %B, %Y")

Format function()

Sys.Date()
format(Sys.Date(), format = "%d %B, %Y")
format(Sys.Date(), format = "Hoje é %A!")

Converting Dates - as.Posixct

date1 <- "Jun 13, '96 hours:23 minutes:01 seconds:45"
date1_convert <- as.POSIXct(date1, format = "%B %d, '%y hours:%H minutes:%M 
seconds:%S")
date1_convert

Operations with Dates

data_de_hoje <- as.Date("2016-06-25", format = "%Y-%m-%d")
data_de_hoje
data_de_hoje + 1

my_time <- as.POSIXct("2016-05-14 11:24:134")
my_time
my_time + 1

data_de_hoje - as.Date(my_time)
data_de_hoje - my_time

Converting Date to specific format

dts = c(1127056501,1104295502,1129233601,1113547501,1119826801,
1132519502,1125298801,1113289201)
mydates = dts
class(mydates) = c('POSIXt','POSIXct')
mydates

mydates = structure(dts,class=c('POSIXt','POSIXct'))
mydates

Isodate function

b1 = ISOdate(2011,3,23)
b1
b2 = ISOdate(2012,9,19)
b2
b2 - b1

difftime(b2, b1, units='weeks')

Lubricating package

ymd("20160604") 
mdy("06-04-2016") 
dmy("04/06/2016")

chegada <- ymd_hms("2016-06-04 12:00:00", tz = "Pacific/Auckland")
partida <- ymd_hms("2011-08-10 14:00:00", tz = "Pacific/Auckland")

chegada
partida

second(chegada)
second(chegada) <- 23
chegada
wday(chegada)
wday(chegada, label = TRUE)
class(chegada)
interval(chegada, partida)

tm1.lub <- ymd_hms("2016-05-24 23:55:26")
tm1.lub

tm2.lub <- mdy_hm("05/25/16 08:32")
tm2.lub

year(tm1.lub)
week(tm1.lub)

tm1.dechr <- hour(tm1.lub) + minute(tm1.lub)/60 + second(tm1.lub)/3600
tm1.dechr
force_tz(tm1.lub, "Pacific/Auckland")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.