How to convert from Moment to Luxon for two letter day of week format? - format

I am converting a typescript app to use Luxon instead of Moment for datetime processing and am not sure how to use Luxon's built-in features (or configurable options) to return the day of week as two letters.
Moment:
moment().format('MM/dd/y') should return '04/Tu/2022'.
Luxon:
DateTime.now().toFormat('MM/ccc/yyyy') but that gives me '04/Tue/2022', which does not fit the required backend data format.
Is there an options parameter I can set to specify the number of letters to return for the day of week string? Or another approach?
This is an example that I found that allows you to specify 2 digit day and month using options...
DateTime.now().toLocaleString({ day: '2-digit', month: '2-digit', year: 'numeric' }) => 05/10/2022

I fear that this is not possible "natively" with Luxon, Table of tokens lists only ccc (day of the week, as an abbreviate localized string), cccc (day of the week, as an unabbreviated localized string), ccccc (day of the week, as a single localized letter) that maps exactly the possible values ('narrow', 'short', 'long') of weekday key of the Intl.DateTimeFormat option object.
Possible workaround is to use custom function and get only first two digit of day of the week.
const DateTime = luxon.DateTime;
function customFormat(dt) {
const year = dt.year;
const month = dt.toFormat('MM');
const dow = dt.toFormat('ccc').substring(0, 2);
return month + '/' + dow + '/' + year;
}
console.log(customFormat(DateTime.now()));
console.log(customFormat(DateTime.fromISO('2022-05-11')));
console.log(customFormat(DateTime.fromISO('2022-05-10')));
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>

Related

What does this d3 time format function mean?

I know there is a specified d3 time formatting. But when I check the example shown here d.Year = new Date(d.Year,0,1); the year's format is "1996"
Does it one of the other ways to format year as a string here, i don't quite understand it.
Also, if my time format is like"01/01/96", what is the right format to get the string here?
This isn't a d3 date formatting function. Its one of the basic forms of the Javascript Date constructor.
Specifically, it is using the multi-parameter form of the constructor:
new Date(year, month, day, hour, minute, second, millisecond);
except that all the time parameters are left as their default (0/midnight) values. If the original value for d.Year is the string "1996", the line
d.Year = new Date(d.Year,0,1);
creates a new Date object with year 1996 (the conversion from string to number will be automatic), month value zero (January), day value 1, and time midnight.

Collecting a range of keys from a dictionary

I am stumped. I have a Timesheet class that holds work days in a dictionary called self.timesheet with dates as the keys and hours, rate as values. I am trying to write a function that can show all the entries in a user defined range of dates.
for now lets assume the key dates are simple integers 20 - 25. i tried this and it didn't do anything at all. no errors, just nothing.
def show_days(self):
date_from = input("From date: ")
date_to = input("To date: ")
t = self.timesheet
for dates in t[date]:
range(date_from, date to)
print(dates)
I can see this doesn't look right , I feel I need *for dates in range(date_from, date_to)* but I can't figure how to get it to loop over the dictionary keys like that.
You need to loop over the range, then check if that key is in the dictionary:
for day in range(date_from, date_to + 1):
if day in t:
print day, t[day]
Note that the values produced by range() do not include the end point, so I used date_to + 1 to ensure it is included anyway.
If your keys are not integers but, say, datetime.date objects, you'll have to construct some kind of loop with datetime.timedelta() to iterate over all dates between two values:
date = date_from = datetime.date(2012, 1, 15)
date_to = datetime.date(2012, 3, 12)
while date <= date_to:
if date in t:
print date, t[date]
date += datetime.timedelta(days=1)

d3.js - Timestamp based on year and week

I have a column with data year&week. I would like to create a date axis based on this.
when i try to format the date, it is not working
I tried this:
var parseDate = d3.time.format("%Y-%W").parse;
d3.csv("temperatures_groups_2.csv", function(error, data) {
data.forEach(function(d,i) {
d.timestamp = parseDate(d.timestamp);
alert(d.timestamp);
d.temperature = +d.temperature;
}
});
i get null when the alert executes.
If i replace the same with d3.time.format("%Y-%m").parse, it works fine. It takes the month but not week no.
The csv contains data like this
timestamp,temperature
2013-01,19.5
2014-02,21.5
2015-03,20.5
2016-04,20.5
2017-05,21.25
The documentation says:
The following directives are not yet supported for parsing:
%U - week number of the year.
%W - week number of the year.
Hence you can't use D3 to parse these dates. You'll have to parse the date yourself or use another library, for example moment.js.
This works since version 3.2 and the documentation has been updated.
%U - week number of the year (Sunday as the first day of the week) as a decimal number [00,53].
%W - week number of the year (Monday as the first day of the week) as a decimal number [00,53].
However, the format in your question won't work because the parser requires a specific date to be specified. E.g. by "%a". It would be nice to assume Sunday in the absence of a date specifier, but that's for another PR.

Determine the amount of clicks required to reach a specific date using a JQuery calendar?

I have a jquery calendar for the start date of a project.
Using Watir (automated browser driver, a gem for ruby), I have a set date that I would like to enter in.
The calendar start date is always today's date, whatever that may be for the day it is used. I was wondering if there was a way that ruby can process what today's date is, and use the specified date provided by the user, to calculate the difference of months between them.
Here is an example of the Calendar plugin: http://jqueryui.com/datepicker/
example:
today's date is 30/10/2012, if there was a project that were to start on the 20/12/2012, that would be 2 months from now, so 2 clicks on the next month button.
Is there a way I could do this?
Here is how I approached a similar situation with JSdatepicker:
$today = Time.now.strftime("%e").gsub(" ", "") #one digit day of month without leading space
#browser.text_field(:id => /dateAvailable/).click
Watir::Wait.until(60) {#browser.div(:id => /dateAvailable_popup_cal/).td(:text => $today).exists?}
#browser.div(:id => /dateAvailable_popup_cal/).td(:text => $today).click
Set or grab the date.
Click the text_field that fires the JSDatePicker object
Wait until the calendar actually pops up
The current month is shown, so choose today's date number.
In your case, you also need to set the month. Whether prompting the user for this, or choosing "today", the theory is the same:
$month = Date::MONTHNAMES[Date.today.month] #etc
Pseudo-code making lots of assumptions (only future dates, month name shown on calendar as text, etc):
while !#jquerytablewindow.text.include?($month)
next_month_button.click
end
I don't see a specific advantage to my method versus counting each month, unless of course we add a month to the calendar one day and you still want your code to work!
You could do:
#End date converted to date object
specified_date = '20/12/2012'
end_date = Date.parse(specified_date)
#Start date (today - 30/10/2012)
today = Date.today
#Determine difference in months
number_of_months_up_to_today = (today.month + today.year * 12)
number_of_months_up_to_end = (end_date.month + end_date.year * 12)
clicks_required = number_of_months_up_to_end - number_of_months_up_to_today
#=> 2
Basically it is counting the number of months since the year 0 and then finding the difference.

Get the the most recent and the one before the most recent item

I have a table with many anniversaries : Date + Name.
I want to display the next anniversary and the one after with Linq.
How can i build the query ?
I use EF
Thanks
John
Just order by date and then use the .Take(n) functionality
Example with a list of some objects assuming you want to order by Date then Name:
List<Anniversaries> annivDates = GetAnnivDates();
List<Anniversaries> recentAnniv = annivDates.OrderBy(d => d.Date).ThenBy(d => d.Name).Take(2).ToList();
If the anniversaries are stored in regular DateTime structs, they may have the 'wrong' year set (i.e. wedding or birth year). I suggest writing a function which calculates the next date for an anniversary (based on the current day) like:
static DateTime CalcNext(DateTime anniversary) {
DateTime newDate = new DateTime(DateTime.Now.Year, anniversary.Month, anniversary.Day);
if (newDate < DateTime.Now.Date)
newDate = newDate.AddYear(1);
return newDate;
}
Then you proceed with sorting the dates and taking the first two values like described in the other postings:
(from e in anniversaries orderby CalcNext(e.Date) select e).Take(2)

Resources