Change date time to specific format - flutter

I've a Date Time string as 27th October 2020 07:30 PM.
And I want it in format yyyy-mm-dd 07:30 PM.
Is there a way to convert in flutter?

If you import the intl package from pub.dev you can format the date any way you like.
https://pub.dev/packages/intl
You can see here in the docs where all the available premade datetime formatting constructors are. https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html
ICU Name Skeleton
-------- --------
DAY d
ABBR_WEEKDAY E
WEEKDAY EEEE
ABBR_STANDALONE_MONTH LLL
STANDALONE_MONTH LLLL
NUM_MONTH M
NUM_MONTH_DAY Md
NUM_MONTH_WEEKDAY_DAY MEd
ABBR_MONTH MMM
ABBR_MONTH_DAY MMMd
ABBR_MONTH_WEEKDAY_DAY MMMEd
MONTH MMMM
MONTH_DAY MMMMd
MONTH_WEEKDAY_DAY MMMMEEEEd
ABBR_QUARTER QQQ
QUARTER QQQQ
YEAR y
YEAR_NUM_MONTH yM
YEAR_NUM_MONTH_DAY yMd
YEAR_NUM_MONTH_WEEKDAY_DAY yMEd
YEAR_ABBR_MONTH yMMM
YEAR_ABBR_MONTH_DAY yMMMd
YEAR_ABBR_MONTH_WEEKDAY_DAY yMMMEd
YEAR_MONTH yMMMM
YEAR_MONTH_DAY yMMMMd
YEAR_MONTH_WEEKDAY_DAY yMMMMEEEEd
YEAR_ABBR_QUARTER yQQQ
YEAR_QUARTER yQQQQ
HOUR24 H
HOUR24_MINUTE Hm
HOUR24_MINUTE_SECOND Hms
HOUR j
HOUR_MINUTE jm
HOUR_MINUTE_SECOND jms
HOUR_MINUTE_GENERIC_TZ jmv
HOUR_MINUTE_TZ jmz
HOUR_GENERIC_TZ jv
HOUR_TZ jz
MINUTE m
MINUTE_SECOND ms
SECOND s
You can also make your own formatting however you like. For example, I have my formatting like so:
DateFormat.yMMMd().format(transactions[index].date),

Do you have a DateTime type or a String type ?
DateTime date = DateTime.now();
print(date.toString())
Cause I get what you want with that code.
console :
2020-10-23 15:36:11.846645

Related

how to find difference btw 21:00 pm - 1:00 am in flutter

can someone please humbly help me? im fining it extremely confusing.
the dates are like this
1st date ~ 2 May 21:00
2nd date ~ 3 May 04:00
Let's say we have two dates.
var d1 = '2 May 04:00';
var d2 = '3 May 05:00';
First, add this intl package to your project and import it
import 'package:intl/intl.dart';
Next, you need to set a dateformat for your dates above:
var dFormat = DateFormat('d MMM HH:mm');
The date format should be same as your dates orelse you will get an error.
Next, convert the datetime string to your desired format.
var dateTime1 = dFormat.parse(d1, true);
var dateTime2 = dFormat.parse(d2, true);
You can set true if you want UTC time.
Lastly, to get difference between two dates just do the following:
print(dateTime2.difference(dateTime1).inHours); //25
The above print statement will return 25 as the difference between both the given time is 25hours. You can get difference in Minutes too.
Happy Coding!

Convert E, d M y HH:mm:ss x string date format to yyyy-mm-dd with databricks

everything is in the title. I tried this without success so far:
date_format(to_date(col("data")["createdAt"], 'E, d M y HH:mm:ss x'), 'yyyy-mm-dd')
all values returned are null
This is an example of input Fri, 12 Jun 20 07:49:17 +0000
to_date does not accept weekdays (E), so you need to remove that using split first. Also you need to fix the formats (note capitalization):
date_format(to_date(split(col("data")["createdAt"], ', ')[1], 'd MMM yy HH:mm:ss Z'), 'yyyy-MM-dd')

Sas changing of date format

I have three columns with date formatted differently in SAS:
12 june 2017 00:15 - full date
2016 - only year
12 - only month
I Need to change the format of date and subtract after the dates to get results in the number of months.
for instance, "12 June 2017 00:15" - December 2016 = 7
how to do it?
As you have probably already found, there isn't a ready-made SAS date informat that will correctly handle your full date field, so you'll need to write a bit of custom logic to convert it before doing your calculation. date9. is the closest matching format I could find:
data example;
fulldate = '12 june 2017 00:15';
year = 2016;
month = 12;
/* Convert string to date9 format and input */
fulldate_num = input(
cats(
scan(fulldate,1),
substr(scan(fulldate,2,' '),1,3),
scan(fulldate,3)
), date9.
);
/* Calculate difference in months */
monthdiff = intck('month', mdy(month,1,year), fulldate_num);
run;
Convert the "full date" field to a SAS date value.
Convert the combo of year and month to a SAS date value, too.
Use the INTCK function to find the difference in months.
For example:
data dates ;
input dt $18. yy mm ;
mm_diff = intck ("mon", input (cats (yy, mm), yymmn6.), input (dt, anydtdte12.)) ;
put mm_diff= ;
cards ;
12 june 2017 00:15 2016 12
11 june 2018 00:15 2017 3
;
run ;
The log will print:
mm_diff=6
mm_diff=15
As a side note, the statement "there isn't a ready-made SAS date informat that will correctly handle your full date field" made elsewhere in this thread is incorrect. As the program snippet above shows, the ANYDTDTEw. informat handles it with aplomb. It's just incumbent upon the programmer to supply a sufficient informat width W. Above, it is selected as W=12. If you're reluctant to guess and/or count, just use ANYDTDTE32.
Regards,
Paul Dorfman
Assuming that you have three numeric variables and the first one contains valid SAS datetime values you should first convert both to valid SAS date values. You can then use the INTCK() function to count months.
nmonths = intck('month',datepart(VAR1),mdy(VAR3,1,VAR2));

Convert Julian Dates to Gregorian dates in impala

I have a table where the dates are in Julian dates, and I would like to convert these dates into Calendar dates.
Here is a sample of a Julian date I have: 2457395.
In calendar date it should be: 07012016 (DDMMYYYY).
I tried to convert the date into the number of days since (01/01/1900) but even if I don't know how to have a date with a number of days as an input.
Many thanks in advance!
Impala does not support dates, only timestamps; and it does not have many date/time formatting features; so you need to be creative, e.g.
select JULIAN_DATE,
adddate('1900-01-01 00:00:00Z', JULIAN_DATE -2415021) as AS_TIMESTAMP,
to_date(adddate('1900-01-01 00:00:00Z', JULIAN_DATE -2415021)) as AS_ISO_DATE_STRING,
from_unixtime(unix_timestamp(adddate('1900-01-01 00:00:00Z', JULIAN_DATE -2415021)), "ddMMyyyy") as AS_DMY_DATE_STRING
from WHATEVER
Sample output:
julian_date as_timestamp as_iso_date_string as_dmy_date_string
----------- ------------------- ------------------ ------------------
2457395 2016-01-07 00:00:00 2016-01-07 07012016
Well if it is a JDE Julian date for example 118163 for 12-06-2018 then below code can be used
date_add(to_timestamp(concat(substr(cast(cast(118162 + 1900000 as int) as string),1,4),'01','01'),'yyyyMMdd'),
cast(substr(cast(cast(118162 + 1900000 as int) as string),5,3) as int)-1)

What is the precise definition of JDE's Julian Date format?

I am writing code to convert from a Gregorian date to a JDE (J.D.Edwards) Julian date.
Note: a JDE Julian date is different from the normal usage of the term Julian date.
As far as I can work out from Googling, the definition of a JDE Julian date is:
1000*(year-1900) + dayofyear
where year is the 4-digit year (e.g. 2009), and dayofyear is 1 for 1st January, and counts up all year to either 365 or 366 for 31st December (depending whether this is a leap year).
My question is this: are years before 1900 supported? If so, does the above formula still hold, or should it be this:
1000*(year-1900) - dayofyear
(note minus instead of plus.)
or something else?
Does anyone have a link to the official documentation for this date format?
The JDE Julian date consists of CYYDDD which is Century, Year, Day of year.
Century is zero for 20th e.g. 19XX and one for 21st e.g. 20XX.
The year is two digits.
So 101001 is 1 January 2001
As you can see this will not support dates before 1900.
See this Oracle page for a simple and official explanation: About the Julian Date Format
The "JDE Julian Date Converter" does return a negative value for:
1809/07/23 : -90635
As opposed to the classical Julian Date:
The Julian date for CE 1809 July 23 00:00:00.0 UT is
JD 2381986.50000
Here is a example of JD EDWARDS (AS/400 software) Julian Date, but that is not an "official" documentation and it does not seems to support dates before 1900...
Note: this "ACC: How to Convert Julian Days to Dates in Access and Back" does not support date before 1900 either... as it speaks about an "informal" Julian day, commonly used by government agencies and contractors.
The informal Julian day format used in this article is the ordinal day of a year (for example, Julian day 032 represents February 1st, or the 32nd day of the year).
Variations on informal Julian day formats include using a preceding two-digit year (for example 96032 for 2/1/96) and separating the year with a dash (for example 96-032).
Another, less popular, Julian day format uses a one digit year (for example 6-032). These additional formats do not uniquely identify the century or decade. You should carefully consider the consequences when using these formats; for example, the Julian day 00061 can be interpreted as 3/1/2000 or 3/2/1900.
Update: Sorry, JDE is probably something else. But for reference:
The JDE I know is different. From page 59 in the book
"Astronomical algorithms" (Jean Meeus, ISBN 0-943396-35-2):
"If the JD corresponds to an instant
measured in the scale of Dynamical
Time (or Ephemeris Time), the
expression Julian Ephemeris Day
(JDE) is generally used. (Not JED as
it is sometimes written. The 'E' is a
sort of index appended to 'JD')"
JD and JDE (for the same point in time) are close in value
as the difference UT and ET is on the order of minutes. E.g. ET-UT was 56.86 seconds in 1990 and -2.72 seconds in 1900.
There is also MJD (Modified Julian Day):
MJD = JD - 2400000.5
Zero point for MJD is 1858-11-17, 0h UT.
Note that JD as Julian date is a misnomer. It is
Julian day. The JD has nothing to do with the Julian
calendar. (This is in disagreement with the Wikipedia article, this
is from the author of the book mentioned above, Jean Meeus - a Belgian astronomer specializing in celestial mechanics.)
Maybe off from the question, you can convert in Excel using the following formula:
Convert Julian to Date in Excel
In Cell A2 place a Julian date, like 102324
in Cell B2 place this formula: (copy it in)
=DATE(YEAR("01/01/"&TEXT(1900+INT(A2/1000),0)),MONTH("01/01/"&TEXT(1900+INT(A2/1000),0)),DAY("01/01/"&TEXT(1900+INT(A2/1000),0)))+MOD(A2,1000)-1
The date 11/20/02 date will appear in cell B2
Convert Date to Julian in Excel
In Cell C2 copy this formula:
=(YEAR(B2)-2000+100)*1000+B2-DATE(YEAR(B2),"01","01")+1
This will convert B2 back to 102324
Save the below source code in a source member called JDEDATES. Use the runsqlstm on the first line to create the functions. You can then do things like
select jde2date(A1UPMJ), f.* from f00095 f
and see a real date.
Source:
--RUNSQLSTM SRCFILE(qtxtsrc) SRCMBR(JDEDATES) COMMIT(*NONE) NAMING(*SQL)
-- jde 2 date
create function QGPL/jde2date ( d decimal(7,0))
returns date
language sql
deterministic
contains sql
SET OPTION DATFMT=*ISO
BEGIN
if d=0 then return null;
else
return date(digits(decimal(d+1900000,7,0)));
end if;
end; -- date 2 jde
create function QGPL/date2jde ( d date)
returns decimal(7,0)
language sql
deterministic
contains sql
SET OPTION DATFMT=*ISO
BEGIN
if d is null then return 0;
else
return (YEAR(D)-1900)*1000+DAYOFYEAR(D);
end if;
end ;
Several years late to the party, but for other folks like me that find yourselves working with legacy systems like this, I hope some of my java snippets can help. I'm leveraging the fact that you can convert this CYYDDD format into yyyyDDD format and parse based on that.
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.SimpleDateFormat;
String jdeJulianDate = "099365"; //Testing with December 31, 1999
// Compile what the year number is
int centIndex = Integer.parseInt(jdeJulianDate.substring(0,1));
int yearIndex = Integer.parseInt(jdeJulianDate.substring(1,3));
int yearNumber = 1900 + (100 * centIndex) + yearIndex;
// Put the year number together with date ordinal to get yyyyDDD format
String fullDate = String.valueOf(yearNumber) + jdeJulianDate.substring(3,6);
// Date parsing, so need to wrap in try/catch block
try {
Date dt = new SimpleDateFormat("yyyyDDD").parse(fullDate);
// Validate it parses to a date in the same year...
Calendar cal = new GregorianCalendar();
cal.setTime(dt);
if (cal.get(Calendar.YEAR) != yearNumber) {
// Cases happen where things like 121366 (should be invalid) get parsed, yielding 2022-01-01.
// Throw exception or what-not here.
}
}
catch (Exception e) {
// Date parsing error handling here
}
A sample of VBA code to convert back and forth between JDE Julian Date and Gregorian:
Public Const Epoch = 1900
Public Const JDateMultiplier = 1000
Public Const FirstJan = "01/01/"
Public Function Julian2Date(ByVal vDate As Long) As Date
Dim Year As Long
Dim Days As Long
Dim SeedDate As Date
' Day Number
Days = vDate - (Int(vDate / JDateMultiplier) * JDateMultiplier) - 1
' Calendar Year
Year = ((vDate - Days) / JDateMultiplier) + Epoch
' First Day of Calendar Year
SeedDate = CDate(FirstJan + CStr(Year))
' Add Number of Days to First Day in Calendar Year
Julian2Date = DateAdd("d", Days, SeedDate)
End Function
Public Function Date2Julian(ByVal vDate As Date) As Long
Dim JYear As String
Dim BeginDate As Date
Dim JDays As Long
' Calendar Year
JYear = Format(Year(vDate), "0000")
' First Day of Calendar Year
BeginDate = CDate(FirstJan + JYear)
' Day Number
JDays = DateDiff("d", BeginDate, vDate) + 1
' Add Number of Days to Year Number
Date2Julian = ((CLng(JYear) - Epoch) * JDateMultiplier) + JDays
End Function
I have tried to make it as clear and simple as possible, and to this end I have intentionally left out any error trapping. However, you should be able to add the code to a VBA module and call them directly from your own code.
I also include some useful snippets of T-SQL:
Todays Date as JDE Julian Date:
(datepart(yy,getdate())-1900) * 1000 + datepart(dy, getdate())
Convert JDE Julian Date to Gregorian (DD/MM/YYYY), replace XXXXXX with the column name containing the JDE Julian Date:
convert (varchar, dateadd (day,convert (int, right(XXXXXX,3)) - 1, convert (datetime, ('1/1/' + convert ( varchar, (cast(left(right(XXXXXX+1000000,6),3) as varchar) + 1900))))),103)
If you require a different Gregorian format, replace the 103 value (right at the end) with the applicable value found here: https://msdn.microsoft.com/en-us/library/ms187928.aspx
I have an easy way for C using time now and epoch 1970, 01, 01 midnight if anybody is interested.
But this is for Julian Day Numbers which is not the same as JDE but they are similar in respect to using math to compute days and I'm sure this idea could be adapted for JDE. Sometimes people just confuse the two like I do. Sorry. But still this is an example of using a time reference which should always be done and since most computers use this it would be just as easy for us not to get too bogged down in dates and just use days before or after this epoch.
Since JDE is now owned by Oracle, they also now support Julian_Day. see:
https://docs.oracle.com/javase/8/docs/api/java/time/temporal/JulianFields.html
#include <stdio.h>
#include <time.h>
#define EPOCH (double) 2440587.5 /* Julian Day number for Jan. 01, 1970 midnight */
int main ()
{
double days = time(0)/86400.0;
printf ("%f days since January 1, 1970\n", days);
printf ("%f\n", days + EPOCH);
return 0;
}
Wow, there's a lot of complicated code in some of these answers just to convert to and from JDE julian dates. There are simple ways in Excel and VBA to get there.
FROM JULIAN
Excel (assuming julian date is in A1):
=DATE(1900+LEFT(A1,LEN(A1)-3),1,RIGHT(A1,3))
VBA (from julian date, j, stored as String):
d = DateSerial(1900 + Left$(j, Len(j) - 3), 1, Right$(j, 3))
VBA (from julian date, j, stored as Long):
d = DateSerial(1900 + Left$(j, Len(CStr(j)) - 3), 1, Right$(j, 3))
TO JULIAN
Excel (assuming date is in A1):
=(YEAR(A1)-1900)*1000+A1-DATE(YEAR(A1),1,0)
VBA (to a Long, j):
j = (Year(d) - 1900) * 1000 + DatePart("y", d)