Convert month (date) into full month (string) - spss

I have a date field (adate10) called Period with values like 07/02/2018.
I need to convert this into a string like "July 2, 2018."
I can use the code below to produce a string, Period_String, that says "JUL 2, 2018." I can't figure out how to format the month as the full month, e.g., "July." Is the only option to create an if/then statement that says 'if 1 then "January", if 2, then "February"', etc.? Was hoping there was a built in format but can't find it.
* extract each date element, then format.
compute mo = xdate.month(Period).
compute da = xdate.mday(Period).
compute yr = xdate.year(Period).
formats mo (month) da yr (F4.0).
execute.
* concatentate date elements as strings.
string Period_String (a30).
compute Period_String = concat(ltrim(string(mo,month)), " ", ltrim(string(da,F4)), ", ", ltrim(string(yr,F4))).
execute.

see the revised month format (month9 instead of month):
compute Period_String = concat(ltrim(string(mo,month9)), " ", ltrim(string(da,F4)), ", ", ltrim(string(yr,F4))).
EDIT:
using #mirirai's one-liner suggestion + getting only first letter in capitals:
string Period_String(a20).
COMPUTE Period_String = CONCAT(
RTRIM(char.substr(STRING(period,MONTH9),1,1)),
RTRIM(lower(char.substr(STRING(period,MONTH9),2)))," ",
LTRIM(STRING(XDATE.MDAY(Period),F2)), ", ",
LTRIM(STRING(XDATE.YEAR(period),F4))
).

Related

Input Date to Written form

I'm trying to write an inputted date from the adjacent cell.
i.e. date entered in A1, B1 = A1 but in word form (in Spanish).
I'm close to a solution but I'm sure making things more complicated than they need to be.
The date column(A) is currently being separated into 3;
day(B), month(D), year(F), using; =LEFT($A1;2), =MID($A1;4;2), =RIGHT($A1;4) accordingly.
In Columns C,E,G I'm using
=INDEX(IMPORTXML("https://www.buscapalabra.com/numeros-a-letras.html?cifra="&B1;"//li[1]//strong");1)
where B1 changes to E1, F1 depending on the column
finally in column H where the date will be written I use:
=trim(LOwer($C2&"de "&TEXT($D2;"mmmm")&" de "&$F2))&If(Value(Right($E2;1))=1;If(Value(MID($E2;4;1))<>1;"o";"");"")
For some reason the text value returned from the site has a space on the end and also does not properly word numbers ending in 1. Hence the use of TRIM() & the nested IF()
It works for now, I haven't tested it exhaustedly but I'd love to hear what people think, I'm new to Sheets but there has to be a cleaner way to do this.
Use custom functions:
You could do this with an Apps Script Custom Function.
First, open a bound script by selecting Tools > Script editor, and copy the following functions to the script (check inline comments for more information):
function DATE_IN_WORDS(date) {
const day = date.getDate(); // Day of the month in numbers
const year = date.getFullYear(); // Year in numbers
const options = { month: 'long'};
const month = new Intl.DateTimeFormat('es-ES', options).format(date); // Month in words
const dayAndYear = [day, year].map(number => { // Retrieve word for day and year
const url = "https://www.buscapalabra.com/numeros-a-letras.html?cifra=" + number
const resp = UrlFetchApp.fetch(url).getContentText();
const first = "<em>Como sustantivo:</em> El <strong>";
const cut = resp.substring(resp.indexOf(first), resp.length);
let word = cut.substring(first.length, cut.indexOf("</strong>")); // Get desired section of HTML
if (number.toString().slice(-1) == "1" && number.toString().slice(-2) != "11") {
word = word.trim() + "o"; // Replace "un" with "uno"
let arrayWord = word.split(" ");
let lastWord = arrayWord.pop();
if (lastWord === "veintiúno") { // If 21, replace "ú" with "u"
lastWord = lastWord.replace("ú", "u");
arrayWord.push(lastWord);
word = arrayWord.join(" ");
}
return word;
} else return word;
});
return dayAndYear.join("de " + month + " de "); // Join day, month and year
}
This function uses:
Intl.DateTimeFormat to retrieve the month in Spanish.
UrlFetchApp to fetch information from https://www.buscapalabra.com/numeros-a-letras.html.
Once it is defined, you can use the function DATE_IN_WORDS the same you would any sheets built-in function. This function would accept the cell with the Date as a parameter.
Example:
Reference:
Custom Functions in Google Sheets

Subtract date in grails filter (le) in filter grails gorm [duplicate]

Is there a way in Groovy to get the duration between two Date objects? The duration format I'm looking for would be something like: 2 days, 10 hours, 30 minutes...
Thanks
TimeCategory has some methods for getting a duration. You could use it like
use(groovy.time.TimeCategory) {
def duration = date1 - date2
print "Days: ${duration.days}, Hours: ${duration.hours}, etc."
}
The use()-Syntax is weird to me.
so I prefer it like this:
def duration = groovy.time.TimeCategory.minus(
new Date(),
new Date(session.creationTime)
);
def values = [
"seconds: " + duration.seconds,
"min: " + duration.minutes,
"hours: " + duration.hours,
"days: " + duration.days,
"ago: " + duration.ago,
];

abas-ERP (FO- Language): Getting Weekday of abas date

Is there any FO function for getting the weekday of an abas date as short. e.g.:
Today: 07.04.2016 -> Thursday (th) ?
In example, when you have;
.type GD xddate ? _F|defined(U|xddate)
.type int xidate ? _F|defined(U|xidate)
..
!START
.formula U|xddate = "."
.formula U|xidate = U|xddate//7
.println 'F|tostring(U|xidate)'
The variable U|xtdate will continue "4" which is the fourth day of the week, Thursday.
Another deeper approach would be, to get the name of the weekday from the built-in dictionary.
See in HOMEDIR/msg.cc.dic which number monday has (in my case 420)
Then this FO-line
.type text xtweekday
.type GD xddate
.formula U|xddate = "09.04.2016"
.atext -language E xtweekday 'F|eval(420 + U|xddate//7)'
'xtweekday' returns Saturday
for "today" just write
.atext -language E xtweekday 'F|eval(420 + G|date//7)'
You can also use the more powerfull .translate command, but this is in this case not really necessary.

Checking variables exist before building an array

I am generating a string from a number of components (title, authors, journal, year, journal volume, journal pages). The idea is that the string will be a citation as so:
#citation = article_title + " " + authors + ". " + journal + " " + year + ";" + journal_volume + ":" + journal_pages
I am guessing that some components occasionally do not exist. I am getting this error:
no implicit conversion of nil into String
Is this indicating that it is trying to build the string and one of the components is nil? If so, is there a neat way to build a string from an array while checking that each element exists to circumvent this issue?
It's easier to use interpolation
#citation = "#{article_title} #{authors}. #{journal} #{year}; #{journal_volume}:#{journal_pages}"
Nils will be substituted as empty strings
array = [
article_title, authors ,journal,
year, journal_volume, journal_pages
]
#citation = "%s %s. %s %s; %s:%s" % array
Use String#% format string method.
Demo
>> "%s: %s" % [ 'fo', nil ]
=> "fo: "
Considering that you presumably are doing this for more than one article, you might consider doing it like so:
SEPARATORS = [" ", ". ", " ", ";", ":", ""]
article = ["Ruby for Fun and Profit", "Matz", "Cool Tools for Coders",
2004, 417, nil]
article.map(&:to_s).zip(SEPARATORS).map(&:join).join
# => "Ruby for Fun and Profit Matz. Cool Tools for Coders 2004;417:"

Set default joda time duration/period format in Grails

When using joda time with Grails you can set the default LocalDate format as they have done here. I want to do the same thing with Duration and Period so I don't have to type something like
durationFormatter.print(exampleObject.myDuration)
every time I want to display a duration or period. I want a call to the toString() method of a Duration / Period object and have it print in the desired format.
I have tried to implement PeriodFormatter in my config file much like they did here, but to no avail. Here is what I have so far in my config file:
jodatime {
format.org.joda.time.LocalDate = "yyyy-MM-dd"
format.org.joda.time.PeriodFormatter = { new format.org.joda.time.PeriodFormatterBuilder()
.appendMonths()
.appendSuffix( " month", " months" )
.appendWeeks()
.appendSuffix( " week", " weeks" )
.appendDays()
.appendSuffix( " day", " days" )
.appendHours()
.appendSuffix( " hour", " hours" )
.toFormatter();
}
}
class
ISOPeriodFormat
field
cStandard
this is default period formatter. This field is private static and haven't setter. But you can change this value using java-reflection API
final Field f = ISOPeriodFormat.class.getDeclaredField("cStandard");
f.setAccessible(true);
f.set(null, yourPeriodFormatter);

Resources