Convert a mm-yy string Jan-01 into date format
If you want a date
class, use as.Date:
x <- "Jan-01"
as.Date(paste("01-", x, sep = ""), format = "%d-%b-%y")
If you want a POSIXct
class, consider using lubridate
:
library(lubridate)
x <- "Jan-01"
dmy(paste("01-", x , sep =""))
If you want to just rely on base
:
x <- "Jan-01"
as.POSIXct(paste("01-", x, sep = ""), format = "%d-%b-%y")
How to convert string date (Jan 25, 2021) to y-m-d date (2021-01-01) in python
As you can see in my comment, you just need to use the right matching mask to get it right.
Your date strings are in the format %b %d, %Y
, so you need to use the same mask in strptime()
. With that in mind, a function like this will do the job:
from datetime import datetime
def mdy_to_ymd(d):
return datetime.strptime(d, '%b %d, %Y').strftime('%Y-%m-%d')
And here is a proof of concept:
>>> from datetime import datetime
>>>
>>>
>>> def mdy_to_ymd(d):
... return datetime.strptime(d, '%b %d, %Y').strftime('%Y-%m-%d')
...
>>> mdy_to_ymd('Jan 25, 2021')
'2021-01-25'
>>>
Bear in mind that strptime()
creates a datetime
object from a string
matching a date using masking characters format. Once you get the right mask and the right representation in a datetime
object, you can convert it to the desired format using strftime()
.
For more information, check strftime()
and strptime()
Format Codes.
Convert date from mm/yyyy to mm/yy using either date-fns or moment
Implementation using moment.js
import Moment from "moment";
let datestring = "08/2021";
let date = Moment(datestring, "MMYYYY");
let datedot = date.format("MMMM YYYY");
alert(datedot); // Output - August 2021
Stackblitz - https://stackblitz.com/edit/typescript-kgrhff?file=index.ts
Convert string to date, format: dd.mm.yyyy
The format is case-sensitive ("%y" is ambiguous and system dependent, I believe):
as.Date(D, "%d.%m.%Y")
[1] "1948-12-06"
The help topic ?strptime
has details:
‘%y’ Year without century (00-99). On input, values 00 to 68 are
prefixed by 20 and 69 to 99 by 19 - that is the behaviour
specified by the 2004 and 2008 POSIX standards, but they do
also say ‘it is expected that in a future version the default
century inferred from a 2-digit year will change’.
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; |
How to convert dd/mm/yyyy string into JavaScript Date object?
MM/DD/YYYY format
If you have the MM/DD/YYYY
format which is default for JavaScript, you can simply pass your string to Date(string)
constructor. It will parse it for you.
var dateString = "10/23/2015"; // Oct 23
var dateObject = new Date(dateString);
document.body.innerHTML = dateObject.toString();
Convert string into date format in R
The as.Date()
function will convert a string into date format, and the format of the output will always be in yyyy-mm-dd
format in R (ISO 8601). The format
argument in the as.Date()
function is to specify the date format of the string input. I remember I initially thought it was specifying the output format, but it's the input format (you can change the output format with a subsequent format()
function, however this will convert it back to a string).
Your string looks to be in ddmmyyyy
(%d%m%Y
) format, this should be what you specify as the format argument in as.Date()
. Your format does not include hyphens, so the format
argument should also not include hyphens. Note that ddmmyyyy
, dd-mm-yyyy
, dd/mm/yyyy
, dd.mm.yyyy
are all different date formats, even though the day, month and year are in the same order, they would be converted to date with formats %d%m%Y
, %d-%m-%Y
, %d/%m/%Y
, and %d.%m.%Y
, respectively.
Further advice on working with dates and times is available in the relevant chapters of R for Data Science by Wickham & Grolemund and The R Cookbook by Teetor & Long.
Related Topics
Adding Multiple Columns in a Dplyr Mutate Call
How to Add Expressions to Labels in Facet_Wrap
Error in Na.Fail.Default: Missing Values in Object - But No Missing Values
Are Data Tables with More Than 2^31 Rows Supported in R with the Data Table Package Yet
Getsymbols and Using Lapply, Cl, and Merge to Extract Close Prices
How to Pad a Vector with Na from the Front
Get Names of Column with Max Value for Each Row
How to Print Double Quotes (") in R
Reading and Scanning Ms Word .Doc Files in R
Differencebetween Aes and Aes_String (Ggplot2) in R
Removing Particular Character in a Column in R
How to Automatically Load Data in an R Package
Replacing Negative Values in a Model (System of Odes) with Zero
Contrast Between Label and Background: Determine If Color Is Light or Dark
Reshaping Data to Plot in R Using Ggplot2
Remove Unused Categorical Values Boxplot - R
Data.Table := Assignments When Variable Has Same Name as a Column