Date format does not work in R - date

This is my date column which is on my dateframe:
data <- data.frame(year_month = c("2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01", "2015-05-01", "2015-06-01"))
class(data$year_month)
[1] "factor"
I tried:
data$year_month1<-as.Date(as.factor(data$year_month), format="%b %Y")
I want:
" Jan 2015", "Feb 2015", "Mar 2015", "Apr 2015", "May 2015", "Jun 2105"
What happens:
My column on data$year_month1 comes Empty.
I tried quite a few solutions that I found here but the format does not change at all or it comes empty.
Help, please>

format(as.Date(data$year_month), format="%b %Y")
#first convert to Date, without as.factor (why did you do that?)
#then convert it to character as you want it, with format()

Related

How to convert text to date format in google sheet?

In my google sheet I have a column with dates but its in a text format. here an example what I have:
Oct 01, 2021
Dec 25, 2020
...
...
I want to convert it to a date format
01/10/2021
25/12/2020
....
I need to find the number of days from the dates in this column, by using "date in column" - now(). This does not work with the format "Oct 01, 2021" since its a text, and I am getting an error from Googlesheet.
Thanks in advance
IS
Try this formula in F2:
=ARRAYFORMULA(IFERROR(DATEDIF(
DATE(
RIGHT(E2:E,4),
MATCH(LEFT(E2:E,3),{"Jan";"Feb";"Mar";"Apr";"May";"Jun";"Jul";"Aug";"Sep";"Oct";"Nov";"Dec"},0),
MID(E2:E,5,2)),
NOW(), "D")))
Update
Revised the formula, which goes in F1 and fills the column, to:
={"Days Left";ARRAYFORMULA(
IFERROR(-1 * DATEDIF( DATE( RIGHT(E2:E,4), MATCH(LEFT(E2:E,3),{"Jan";"Feb";"Mar";"Apr";"May";"Jun";"Jul";"Aug";"Sep";"Oct";"Nov";"Dec"},0), MID(E2:E,5,2)), NOW(), "D"),
IFERROR(DATEDIF( NOW(),DATE( RIGHT(E2:E,4), MATCH(LEFT(E2:E,3),{"Jan";"Feb";"Mar";"Apr";"May";"Jun";"Jul";"Aug";"Sep";"Oct";"Nov";"Dec"},0), MID(E2:E,5,2)), "D"))))}
which reverses the date difference values. It also handles date differences for dates either in the future, or in the past.
Use the DATEVALUE() function on a date string, then use DATEDIF() to find the difference between two dates.
=DATEDIF(DATEVALUE("Oct 01, 2020"), DATEVALUE("Dec 25, 2020"), "D")
UPDATE: To find the date between today and a date string in another cell use this example:
=DATEDIF(DATEVALUE(A2), NOW(), "D")
If cell A2 contains string Oct 01, 2020, it will return 70 for today 2020-12-10

Date to String MongoDB

Some of the documents in the collection contains Birthday field like given below -
------- birthday : "Friday, February 7, 2014 at 8:03:22 PM" ------
I want to convert to Date 'DD/MM/YYYY' Format.
using python can someone solve this
You want to use python's strptime, all you need to do is specify the structure of your string like so:
from datetime import datetime
new_date = datetime.strptime("Friday, February 7, 2014 at 8:03:22 PM", '%A, %B %d, %Y at %I:%M:%S %p').strftime('%d/%m/%Y')

Dateparse MON in Tableau

I have a data source that returns a date as a string in the form of 'MON YYYY' (APR 2014, MAY 2014, etc.).
I tried making a calculated field off of this information with the following formula:
DATEPARSE('MMM YYYY', [Field1])
This is a sample set of the data I'm getting (I added the pipe as a divider):
Field1 || Calculated Field
APR 2014 || 12/22/2013
APR 2015 || 12/28/2014
APR 2016 || 12/27/2015
AUG 2014 || 12/22/2013
AUG 2015 || 12/28/2014
AUG 2016 || 12/27/2015
I've also tried to add a day field, but that results in the same incorrect data as above:
DATE(DATEPARSE('dd MMM YYYY','01 ' +[Field1]))
Is there something I'm perhaps misunderstanding about the dateparse function?
It turns out that YYYY means something totally different than yyyy. The capitalized MMMwas necessary for the MON type description. This worked for me:
DATE(DATEPARSE('MMM yyyy',[Field1]))
If you date the date off you'll get the hour, minute, second fields as well.
Dateparse converted it from a string [Field1] into a Date type using the aforementioned format of three digit month, a space, and a four digit year (e.g. AUG 2014 -> 8/2/2014).

How to format a date in Ext JS 3?

I have a date value like this ;
Date {Fri Feb 13 2015 02:00:00 GMT+0200 (GTB Standart Saati)}
I get it from a grid column with ;
selectionModel.getSelected().data['date'];
And column model date format is 'd/m/Y'. It shows in grid well (d/m/Y).But when i get selected value it returns a date format doesn't like 'd/m/Y'. How can i format this date to set to a textfield ?
According to ExtJs Docs
var d = new Date(1993, 6, 28, 14, 39, 7);
println(d.toString()); // prints Wed Jul 28 1993 14:39:07 GMT-0600 (PDT)
println(d.toDateString()); // prints Wed Jul 28 1993
Edit:
Please use Ext.Date.format to format the date:
Ext.Date.format(d,'d/m/Y');
According to ExtJS 3.4 Docs Date object is extended with .format() method.
So you should be able to just do
var d = new Date();
d.format("d/m/Y");

DateTime JodaTime conversion

Please imagine the following:
I've this date in the Azores (GMT -1)
23/10/2010 23:00:00
And i want to convert this date to the following (the GMT +1)
24/10/2010 01:00:00
I want this behavior for any date in any timezone and the Date function with timezone give me the GMT -1 for this case.
Please note that i'm using JodaTime.
Thanks.
DateTime dateTime = new DateTime(2010, 10, 23, 23, 0, 0, 0, DateTimeZone.forOffsetHours(-1)); // (GMT -1) 23/10/2010 23:00:00
DateTime inAnotheTimeZone = dateTime.withZone(DateTimeZone.forOffsetHours(1));