Convert datetime object to a String of date only in Python
You can use strftime to help you format your date.
E.g.,
import datetime
t = datetime.datetime(2012, 2, 23, 0, 0)
t.strftime('%m/%d/%Y')
will yield:
'02/23/2012'
More information about formatting see here
How do I convert a date/time string to a DateTime object in Dart?
DateTime
has a parse
method
var parsedDate = DateTime.parse('1974-03-20 00:00:00.000');
https://api.dartlang.org/stable/dart-core/DateTime/parse.html
Convert date-time string to class Date
You may be overcomplicating things, is there any reason you need the stringr package? You can use as.Date
and its format
argument to specify the input format of your string.
df <- data.frame(Date = c("10/9/2009 0:00:00", "10/15/2009 0:00:00"))
as.Date(df$Date, format = "%m/%d/%Y %H:%M:%S")
# [1] "2009-10-09" "2009-10-15"
Note the Details section of ?as.Date
:
Character strings are processed as far as necessary for the format specified: any trailing characters are ignored
Thus, this also works:
as.Date(df$Date, format = "%m/%d/%Y")
# [1] "2009-10-09" "2009-10-15"
All the conversion specifications that can be used to specify the input format
are found in the Details section in ?strptime
. Make sure that the order of the conversion specification as well as any separators correspond exactly with the format of your input string.
More generally and if you need the time component as well, use as.POSIXct
or strptime
:
as.POSIXct(df$Date, "%m/%d/%Y %H:%M:%S")
strptime(df$Date, "%m/%d/%Y %H:%M:%S")
I'm guessing at what your actual data might look at from the partial results you give.
Change a Java date-time string to date
You need to use the SimpleDateFormat:
String sDate = "2018-01-17 00:00:00";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = df.parse(sDate);
SimpleDateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(df1.format(date));
Go through the SimpleDateFormat class to get into details for this
Fastest way to parse a date-time string to class Date
Note that as.Date
will ignore junk after the date so this takes less than 10 seconds on my not particularly fast laptop:
xx <- rep("10/17/2017 12:00:00 AM", 5000000) # test input
system.time(as.Date(xx, "%m/%d/%Y"))
## user system elapsed
## 9.57 0.20 9.82
how to convert date and time from character to datetime type
As @Richard Scriven pointed out, you shouldn't be using as.Date
because it's not a datetime class. Here are a few different ways:
DateTime <- "2007-02-01 00:00:00"
DateTime2 <- "02/01/2007 00:06:10"
## default format Y-m-d H:M:S
> as.POSIXct(DateTime,tz=Sys.timezone())
[1] "2007-02-01 EST"
> as.POSIXlt(DateTime,tz=Sys.timezone())
[1] "2007-02-01 EST"
##
## specify format m/d/Y H:M:S
> as.POSIXct(DateTime2,format="%m/%d/%Y %H:%M:%S",tz=Sys.timezone())
[1] "2007-02-01 00:06:10 EST"
> as.POSIXlt(DateTime2,format="%m/%d/%Y %H:%M:%S",tz=Sys.timezone())
[1] "2007-02-01 00:06:10 EST"
##
## using lubridate
library(lubridate)
> ymd_hms(DateTime,tz=Sys.timezone())
[1] "2007-02-01 EST"
> mdy_hms(DateTime2,tz=Sys.timezone())
[1] "2007-02-01 00:06:10 EST"
You don't have to specify format=
for as.POSIXct
and as.POSIXlt
when you have the %Y-%m-%d %H:%M:%S
format. In other cases, like %m/%d/%Y %H:%M:%S
, you usually have to specify the format explicitly.
How can I convert a string datetime column to a datetime column in R?
We can just use %T
as.Date(data$timestamp, "%Y-%m-%d %T")
#[1] "2020-08-10" "2020-08-10" "2020-04-15" "2020-01-18" "2020-02-12"
data
data <- structure(list(Id = c("A9", "B9", "G1", "D2", "F8"), timestamp = c("2020-08-10 09:05:01.000000",
"2020-08-10 09:04:18.000000", "2020-04-15 11:05:08.000000", "2020-01-18 19:04:05.000000",
"2020-02-12 08:04:08.000000")), class = "data.frame", row.names = c(NA,
-5L))
converting date time string to date with time in R
Use as.POSIXct
or strptime
:
as.POSIXct("9/2/11 4:20", format="%m/%d/%y %H:%M",tz="GMT")
#[1] "2011-09-02 04:20:00 GMT"
strptime("9/2/11 4:20", format="%m/%d/%y %H:%M",tz="GMT")
#[1] "2011-09-02 04:20:00 GMT"
Class Date
does not contain information on the time of the day by definition.
Java string to date conversion
That's the hard way, and those java.util.Date
setter methods have been deprecated since Java 1.1 (1997). Moreover, the whole java.util.Date
class was de-facto deprecated (discommended) since introduction of java.time
API in Java 8 (2014).
Simply format the date using DateTimeFormatter
with a pattern matching the input string (the tutorial is available here).
In your specific case of "January 2, 2010" as the input string:
- "January" is the full text month, so use the
MMMM
pattern for it - "2" is the short day-of-month, so use the
d
pattern for it. - "2010" is the 4-digit year, so use the
yyyy
pattern for it.
String string = "January 2, 2010";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date); // 2010-01-02
Note: if your format pattern happens to contain the time part as well, then use LocalDateTime#parse(text, formatter)
instead of LocalDate#parse(text, formatter)
. And, if your format pattern happens to contain the time zone as well, then use ZonedDateTime#parse(text, formatter)
instead.
Here's an extract of relevance from the javadoc, listing all available format patterns:
Symbol | Meaning | Presentation | Examples |
---|---|---|---|
G | era | text | AD; Anno Domini; A |
u | year | year | 2004; 04 |
y | year-of-era | year | 2004; 04 |
D | day-of-year | number | 189 |
M /L | month-of-year | number/text | 7; 07; Jul; July; J |
d | day-of-month | number | 10 |
Q /q | quarter-of-year | number/text | 3; 03; Q3; 3rd quarter |
Y | week-based-year | year | 1996; 96 |
w | week-of-week-based-year | number | 27 |
W | week-of-month | number | 4 |
E | day-of-week | text | Tue; Tuesday; T |
e /c | localized day-of-week | number/text | 2; 02; Tue; Tuesday; T |
F | week-of-month | number | 3 |
a | am-pm-of-day | text | PM |
h | clock-hour-of-am-pm (1-12) | number | 12 |
K | hour-of-am-pm (0-11) | number | 0 |
k | clock-hour-of-am-pm (1-24) | number | 0 |
H | hour-of-day (0-23) | number | 0 |
m | minute-of-hour | number | 30 |
s | second-of-minute | number | 55 |
S | fraction-of-second | fraction | 978 |
A | milli-of-day | number | 1234 |
n | nano-of-second | number | 987654321 |
N | nano-of-day | number | 1234000000 |
V | time-zone ID | zone-id | America/Los_Angeles; Z; -08:30 |
z | time-zone name | zone-name | Pacific Standard Time; PST |
O | localized zone-offset | offset-O | GMT+8; GMT+08:00; UTC-08:00; |
X | zone-offset 'Z' for zero | offset-X | Z; -08; -0830; -08:30; -083015; -08:30:15; |
x | zone-offset | offset-x | +0000; -08; -0830; -08:30; -083015; -08:30:15; |
Z | zone-offset | offset-Z | +0000; -0800; -08:00; |
Related Topics
Storing Ggplot Objects in a List from Within Loop in R
Make the Background of a Graph Different Colours in Different Regions
Position Geom_Text on Dodged Barplot
Extract a Dplyr Tbl Column as a Vector
Concatenate a Vector of Strings/Character
Select Groups Based on Number of Unique/Distinct Values
Select the First and Last Row by Group in a Data Frame
Pull Out P-Values and R-Squared from a Linear Regression
Create Grouping Variable For Consecutive Sequences and Split Vector
Update Data Frame Via Function Doesn't Work
Changing Column Names of a Data Frame
Subtract Value from Previous Row by Group