ID EN
Data Types & Classes

as.Date

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Berfungsi untuk mengkonversi antara representasi karakter dan objek kelas "Tanggal" yang mewakili tanggal kalender.

Syntax

R
as.Date(x, …)
# S3 method for character
as.Date(x, format, tryFormats = c("%Y-%m-%d", "%Y/%m/%d"),
        optional = FALSE, …)
# S3 method for numeric
as.Date(x, origin, …)
# S3 method for POSIXct
as.Date(x, tz = "UTC", &#8230;)<p></p><p># S3 method for Date
format(x, &#8230;)</p><p># S3 method for Date
as.character(x, &#8230;)</p>

Arguments

Parameter Deskripsi
x an object to be converted.
format character string. If not specified, it will try tryFormats one by one on the first non-NA element, and give an error if none works. Otherwise, the processing is via strptime.
tryFormats character vector of format strings to try if format is not specified.
optional logical indicating to return NA (instead of signalling an error) if the format guessing does not succeed.
origin a Date object, or something which can be coerced by as.Date(origin, …) to such an object.
tz a time zone name.
&#8230; further arguments to be passed from or to other methods, including format for as.character and as.Date methods.

Return Value

Metode format dan as.character mengembalikan vektor karakter yang mewakili tanggal. Tanggal NA dikembalikan sebagai NA_character_. Metode as.Date mengembalikan objek kelas "Date".

Details

Aturan daur ulang vektor yang biasa diterapkan pada x dan format sehingga jawabannya adalah panjang vektor yang lebih panjang. Konversi spesifik lokal ke dan dari string karakter digunakan jika diperlukan dan tersedia. Hal ini mempengaruhi nama hari dan bulan. Metode as.Date menerima string karakter, faktor, NA logis, dan objek kelas "POSIXlt" dan "POSIXct". (Yang terakhir diubah menjadi hari dengan mengabaikan waktu setelah tengah malam dalam representasi waktu di zona waktu yang ditentukan, default UTC.) Juga objek kelas "tanggal" (dari tanggal paket) dan "tanggal" (dari paket chron). String karakter diproses sejauh diperlukan untuk format yang ditentukan: karakter tambahan apa pun akan diabaikan. as.Date akan menerima data numerik (jumlah hari sejak suatu zaman), tetapi

Contoh

Example
R
# NOT RUN {
## locale-specific version of the date
format(Sys.Date(), "%a %b %d")
# }
# NOT RUN {
## read in date info in format 'ddmmmyyyy'
## This will give NA(s) in some locales; setting the C locale
## as in the commented lines will overcome this on most systems.
## lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
z <- as.Date(x, "%d%b%Y")
## Sys.setlocale("LC_TIME", lct)
z

## read in date/time info in format 'm/d/y'
dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")
as.Date(dates, "%m/%d/%y")

## date given as number of days since 1900-01-01 (a date in 1989)
as.Date(32768, origin = "1900-01-01")
## Excel is said to use 1900-01-01 as day 1 (Windows default) or
## 1904-01-01 as day 0 (Mac default), but this is complicated by Excel
## incorrectly treating 1900 as a leap year.
## So for dates (post-1901) from Windows Excel
as.Date(35981, origin = "1899-12-30") # 1998-07-05
## and Mac Excel
as.Date(34519, origin = "1904-01-01") # 1998-07-05
## (these values come from http://support.microsoft.com/kb/214330)

## Experiment shows that Matlab's origin is 719529 days before ours,
## (it takes the non-existent 0000-01-01 as day 1)
## so Matlab day 734373 can be imported as
as.Date(734373, origin = "1970-01-01") - 719529 # 2010-08-23
## (value from
## http://www.mathworks.de/de/help/matlab/matlab_prog/represent-date-and-times-in-MATLAB.html)

## Time zone effect
z <- ISOdate(2010, 04, 13, c(0,12)) # midnight and midday UTC
as.Date(z) # in UTC
# }
# NOT RUN {
## these time zone names are common
as.Date(z, tz = "NZ")
as.Date(z, tz = "HST") # Hawaii
# }

See Also

Date for details of the date class; locales to query or set a locale. Your system's help pages on strftime and strptime to see how to specify their formats. Windows users will find no help page for strptime: code based on glibc is used (with corrections) so all the format specifiers described here are supported but with no alternative number representation nor era available in any locale.