Elm : How to get YESTERDAY date in String Date - date

Let's say I have this Model:
type alias Model =
{ currentDate : String
, yesterdayDate : String
}
The CurrentDate I got from Html input type date (Date Picker) is in format YYYY-MM-DD
Html Form
input [ name "date", type_ "date", onInput UpdateDate ] []
Update.elm
UpdateDate date ->
let
-- Get Yesterday Date function here
in
( { model | currentDate = date, yesterdayDate = "" }, Cmd.none )
In this situation , how can i get yesterday Date in String ?
My idea is parse the day into INT and using subtraction method to get Yesterday day but I cannot find any way to do it... Any help is appreciate.

Convert the string date to Posix, convert the Posix to milliseconds since epoch, subtract the amount of milliseconds in a day, convert the resulting milliseconds back to Posix and the Posix to an ISO8601 string. Take the first 10 characters from that string.
module Main exposing (main)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
import Iso8601
import Time exposing (Posix)
sampleDate =
"2020-05-01"
subtractDays : Int -> Posix -> Posix
subtractDays days time =
(Time.posixToMillis time - (days * 24 * 60 * 60 * 1000))
|> Time.millisToPosix
subtractDaysFromIsoDate : Int -> String -> String
subtractDaysFromIsoDate days date =
Iso8601.toTime date
|> Result.map (subtractDays days >> Iso8601.fromTime >> String.left 10)
|> Result.withDefault date
main =
text <| subtractDaysFromIsoDate 1 sampleDate
Note that in this implementation if the string is not a valid date it will just be returned unmodified rather than fail. You might want to capture that this operation can fail.

As you can trust that you get a valid string format from html and are aware of the date package, you can split the date string into 3 strings, convert each into an integer and then construct today and yesterday as a Date value.
Questions you should ask yourself:
Do you really want to store the date as a String? The Date type might be more useful if you want to do something else then just display the string value.
And do you really want to store both today and yesterday? The latter can be easily computed when needed.
Example for string splitting:
case
String.split "-" date
|> List.map String.toInt
of
[ Just year, Just monthInt, Just day ] ->
-- convert monthInt to `Month`
-- construct current date
-- add -1 `Day`
Debug.todo "todo" 2
_ ->
Debug.todo "invalid date format" date

Related

How to convert unix timestamp to iso 8601 in Flutter

I am getting date from a server as a unix timestamp, how can I convert it to ISO 8601 date format in flutter?
the date I receive:
1611694800000
How I want to convert it to be
2021-01-26T22:00:00.000+00:00
What I have done so far with no luck
String s = '1611694800000';
debugPrint("Recevied date is: $s");
String dateS = DateTime.parse(s).toIso8601String();
debugPrint("Converted date : $dateS");
String dateStr = (dateS.split(".")[0].split("T")[0] + " 00:00:00").substring(1);
debugPrint("Activation date: $dateStr");
I end up getting:
Unhandled Exception: FormatException: Invalid date format.
Use DateTime.fromMillisecondsSinceEpoch:
var timestampMilliseconds = 1611694800000;
var datetime =
DateTime.fromMillisecondsSinceEpoch(timestampMilliseconds, isUtc: true);
print(datetime.toIso8601String()); // Prints: 2021-01-26T21:00:00.000Z
(Note that the printed time is one hour off of your stated expectation, but I'm assuming that's a mistake in your expectation.)
The reason why you are getting invalid date format is because you have to provide date in string like '2021-04-19' and not milliseconds;
This package makes it easy to format dates time_formatter

Convert the String Monday 5 October to date using Google Apps Script

I need to convert the date string of type WEEKDAY DATE MONTHNAME, Example: from "Monday 5 October" to date object.
I have tried with
Utilities.formatDate(new Date("Monday 5 October"), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'")
How do I convert it, I am ok using V8 apps script engine
The Date constructor accepts timestamp strings formatted according to IETF-compliant RFC 2822 timestamps and ISO8601.
There are many ways to convert your string to date, but probably one of the simplest is appending the current year to your string, using getFullYear():
const source = "Monday 5 October";
const date = new Date(`${source} ${new Date().getFullYear()}`);
Reference:
Date() constructor
IETF-compliant RFC 2822 timestamps

Error java.time.format.DateTimeParseException: could not be parsed, unparsed text found at index 10

I´m trying to pase the next String using LocalDateTime, but I always get de unparsed text found error:
Error java.time.format.DateTimeParseException: Text '2016-08-18 14:27:15.103+02' could not be parsed, unparsed text found at index 10
Here is my String: convertDate: '2016-08-18 14:27:15.103+02'
And my code:
public static LocalDate conversorStringToLocalDateTime(String convertDate) throws ParseException {
LocalDate dateTime =LocalDate.parse(convertDate);
return dateTime;
}
I guess is not too complicated, buy I´m not able to see the error. Could the +02 in the String be the cause?
tl;dr
OffsetDateTime odt = OffsetDateTime.parse ( "2016-08-18 14:27:15.103+02" , DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" ) ) ;
Details
The Answer by greg-449 is correct about the problem (using a date-only object for a date-time value) but not the solution.
That Answer uses LocalDateTime which unnecessarily throws away valuable information about the offset-from-UTC. A LocalDateTime does not represent a specific moment on the timeline, only a vague idea about possible moments depending on adjusting into a particular time zone.
The +02 is an offset-from-UTC meaning “two hours ahead of UTC”. So in UTC the time-of-day for this simultaneous moment is 12 hours, 2 hours less than your 14 hours. This does represent a specific moment on the timeline. This offset is the valuable information you are throwing away with a LocalDateTime rather than an OffsetDateTime.
The format of your string is in SQL format, which is close to standard ISO 8601 format. Merely replace the SPACE in the middle with a T. The java.time classes use ISO 8601 formats by default, so no need to specify a formatting pattern.
String input = "2016-08-18 14:27:15.103+02";
String inputModified = input.replace ( " " , "T" );
Unfortunately, Java 8 has a bug in parsing offset values abbreviated to just an hour or offset values omitting the colon between hours and minutes. Fixed in Java 9. But in Java 8, we need to adjust the input.
// Workaround for Java 8 where 2-digit offset fails parsing. Fixed in Java 9.
int lengthOfAbbreviatedOffset = 3;
if ( inputModified.indexOf ( "+" ) == ( inputModified.length () - lengthOfAbbreviatedOffset ) ) {
// If third character from end is a PLUS SIGN, append ':00'.
inputModified = inputModified + ":00";
}
if ( inputModified.indexOf ( "-" ) == ( inputModified.length () - lengthOfAbbreviatedOffset ) ) {
// If third character from end is a PLUS SIGN, append ':00'.
inputModified = inputModified + ":00";
}
Now parse.
OffsetDateTime odt = OffsetDateTime.parse ( inputModified );
Dump to console. Note how we transformed +02 into +02:00.
System.out.println ( "input: " + input + " | inputModified: " + inputModified + " | odt: " + odt );
input: 2016-08-18 14:27:15.103+02 | inputModified: 2016-08-18T14:27:15.103+02:00 | odt: 2016-08-18T14:27:15.103+02:00
Alternatively, specify a formatting pattern. The offset-parsing bug does not bite when using this formatting pattern.
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" );
OffsetDateTime odt = OffsetDateTime.parse ( input , f );
Database
Coming from Postgres, you should be retrieving the value as a date-time object rather than a String.
If your JDBC driver complies with JDBC 4.2 you can call ResultSet::getObject to get an Instant or OffsetDateTime. If not, call ResultSet::getTimestamp to get a java.sql.Timestamp, then immediately convert to java.time by calling toInstant on the Timestamp object.
Stick with java.time for your business logic; use the java.sql types briefly and only for exchange with the database.
Your code is using LocalDate which only parses a date - not a date and time so you are getting an error when the parse finds the space after the date.
So you should be using LocalDateTime but LocalDateTime.parse(String) expects an ISO format date which is not the format you are using.
So you need to use a DateTimeFormatter to specify the format of your input string. Something like:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX");
LocalDateTime result = LocalDateTime.parse(convertDate, format);

using datetime object as an argument

I have been struggling to understand how to use datetime objects. I want to use datetime.date instances as keys in a dictionary. I then want to be able to return dates within specified ranges using datetime.delta.
My first conundrum is when I create an object to be entered into the dictionary.
class Work_day():
'''input a workday , date and hours worked'''
def __init__(self, date, hours, rate):
self.date = datetime.date()
self.hours = hours
self.rate = rate
I want self.date to be a datetime.date object but datetime.date takes 3 argument (year, month, day) so what is the correct syntax for the def_init_ argument 'date'?
Then I assume when I change how that is written in the Work_day class then I will have to modify my code when I create instances of it in the Timesheet class e.g. in add_work_day() method
class Timesheet():
'''Represent a collection of workdays'''
def __init__(self):
self.timesheet = {}
def add_work_day(self, date, hours,rate):
'''adds a record of a work day into the timesheet dictionary'''
day = Work_day(date, hours, rate)
if day.date in self.timesheet:
print("There is already an entry for this day. ")
else:
self.timesheet[day.date] = hours, rate
I've been researching the python docs and scouring books but I'm not getting it! Need some help.
I also have a method that prints a range of the workdays in the timesheet. I made it work when I subbed the date key for a simple int. here it is (in ''' ''') with a shonky attempt at a datetime delta underneath
def show_days(self):
'''shows a user defined range of dates and the total pay for that period'''
pp = pprint.PrettyPrinter()
date_from = input("From date: ")
date_to = input("To date: ")
t = self.timesheet
total = 0
'''for dates in range(date_from, date_to + 1):
if dates in t:
total += self.sum_day(dates)
pp.pprint((dates, t[dates)])
print("Total £", total)'''
date = date_start = datetime.date(date_from)
date_end = datetime.date(date_to)
while date <= date_end:
if date in t:
print(date, t[dates])
date += datetime.timedelta(days=1)
I hope someone can find the patience to talk me through this. Cheers.
If you assign the date with self.date = datetime.date(*date), then you can create a Work_day by passing a (year,month,day) tuple:
day = Work_day((2013,5,31), 8.0, 8.25)
Alternatively, if you want the input to be a date string, use datetime.strptime, an appropriate formatting string, and the date() method to get a date object:
self.date = datetime.datetime.strptime(date,'%m/%d/%Y').date()
...
date = Work_day('5/31/2013', 8.0, 8.25)
Finally, you could just pass a date object:
day = Work_day(datetime.date(2013,5,31), 8.0, 8.25)
...
self.date = date
The Timesheet class should work after any of these changes. show_days still needs some work, but I'll leave that as an exercise. Hint: Parse the input dates with strptime.

Parsing String to date doesnt work

I tried parsing a string in a namedQuery, but it seems doesnt work. I have this code in my domain class:
searchBirthdaten{ q ->
def dates = Date.parse("yyyyy:MM:dd HH:mm:ss", "2011-9-21 00:00:00")
eq 'birthDate' , dates)
}
But I always got this error:
Unparseable date: "2011-9-21 00:00:00"
I really dont understand why this is happening. Any idea?
Your date input string has to be in the format you defined: yyyy:MM:dd HH:mm:ss (corrected)
So your 3 issues were:
You are using the "-" character to delimit you date for parsing but your format string is using ":"
You have 5 ys in your format string i.e. yyyyy:MM.... Which won't be valid for another 8 thousandish years ;)
You define your month format as MM but you are passing only '9', this will need to be '09' to match your fomat string.