Convert a Mm-Yy String "Jan-01" into Date Format

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:

  1. "January" is the full text month, so use the MMMM pattern for it
  2. "2" is the short day-of-month, so use the d pattern for it.
  3. "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:

































































































































































































SymbolMeaningPresentationExamples
GeratextAD; Anno Domini; A
uyearyear2004; 04
yyear-of-erayear2004; 04
Dday-of-yearnumber189
M/Lmonth-of-yearnumber/text7; 07; Jul; July; J
dday-of-monthnumber10
Q/qquarter-of-yearnumber/text3; 03; Q3; 3rd quarter
Yweek-based-yearyear1996; 96
wweek-of-week-based-yearnumber27
Wweek-of-monthnumber4
Eday-of-weektextTue; Tuesday; T
e/clocalized day-of-weeknumber/text2; 02; Tue; Tuesday; T
Fweek-of-monthnumber3
aam-pm-of-daytextPM
hclock-hour-of-am-pm (1-12)number12
Khour-of-am-pm (0-11)number0
kclock-hour-of-am-pm (1-24)number0
Hhour-of-day (0-23)number0
mminute-of-hournumber30
ssecond-of-minutenumber55
Sfraction-of-secondfraction978
Amilli-of-daynumber1234
nnano-of-secondnumber987654321
Nnano-of-daynumber1234000000
Vtime-zone IDzone-idAmerica/Los_Angeles; Z; -08:30
ztime-zone namezone-namePacific Standard Time; PST
Olocalized zone-offsetoffset-OGMT+8; GMT+08:00; UTC-08:00;
Xzone-offset 'Z' for zerooffset-XZ; -08; -0830; -08:30; -083015; -08:30:15;
xzone-offsetoffset-x+0000; -08; -0830; -08:30; -083015; -08:30:15;
Zzone-offsetoffset-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



Leave a reply



Submit