How to format date string with date-fns without timezone transformation? - format

I have really simple date as string as ISO const mytDate = '2021-10-24T16:00:00.000Z' and I want to format it like format(new Date(myDate), 'yyyy-MM-dd HH:mm:ss') but new Date() cause that it convert it to my local timezone and I do not want it. I would like to take this string date and just format it as it is.
Is there a solution ?
Cheers!

Related

Using Carbon I got "textual month could not be found" error

In my Laravel 5.8 application, I to convert date in string format like
02 May, 2019 to date time using Carbon. I try like :
setlocale(LC_TIME, 'en');
$filter_check_in_datepicker_till = Carbon::createFromFormat( 'dd MMMM YYY', $filter_check_in_datepicker_till )->locale('en_EN');;
But got error:
"message": "Unexpected data found.\nA textual month could not be found\nData missing",
Which is right way?
Thanks!
The Carbon docs say:
createFromFormat() is mostly a wrapper for the base php function DateTime::createFromFormat.
It isn't entirely clear from the docs, but that means the $format parameter passed to createFromFormat() is a DateTime format, not a Carbon format. So instead of dd MMMM YYY, you should use d, M Y (check the DateTime format reference):
\Carbon\Carbon::createFromFormat('d, M Y', '02, May 2019');
// returns 2019-05-02 12:48:26
For reference, there are several problems in your Carbon format, so even if createFromFormat took a Carbon format string what you have would not work. Checking the Carbon format reference:
dd is actually "Minified day name (from Su to Sa), transatable". You really want DD for the zero-padded day of month;
The date format you are using includes a comma, but your format string is missing that;
YYY is not actually a valid Carbon format string. You really wanted YYYY for 4-digit year;

Parsing a year String to a LocalDate with Java8

With Joda library, you can do
DateTimeFormat.forPattern("yyyy").parseLocalDate("2008")
that creates a LocalDate at Jan 1st, 2008
With Java8, you can try to do
LocalDate.parse("2008",DateTimeFormatter.ofPattern("yyyy"))
but that fails to parse:
Text '2008' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2008},ISO of type java.time.format.Parsed
Is there any alternative, instead of specifically writing sth like
LocalDate.ofYearDay(Integer.valueOf("2008"), 1)
?
LocalDate parsing requires that all of the year, month and day are specfied.
You can specify default values for the month and day by using a DateTimeFormatterBuilder and using the parseDefaulting methods:
DateTimeFormatter format = new DateTimeFormatterBuilder()
.appendPattern("yyyy")
.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
.toFormatter();
LocalDate.parse("2008", format);
String yearStr = "2008";
Year year = Year.parse(yearStr);
System.out.println(year);
Output:
2008
If what you need is a way to represent a year, then LocalDate is not the correct class for your purpose. java.time includes a Year class exactly for you. Note that we don’t even need an explicit formatter since obviously your year string is in the default format for a year. And if at a later point you want to convert, that’s easy too. To convert into the first day of the year, like Joda-Time would have given you:
LocalDate date = year.atDay(1);
System.out.println(date);
2008-01-01
In case you find the following more readable, use that instead:
LocalDate date = year.atMonth(Month.JANUARY).atDay(1);
The result is the same.
If you do need a LocalDate from the outset, greg449’s answer is correct and the one that you should use.
I didn't get you
but from the title I think you want to parse a String to a localdate so this is how you do it
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
String date = "16/08/2016";
//convert String to LocalDate
LocalDate localDate = LocalDate.parse(date, formatter);

convert date format in hive

How can I convert this date :
Dec 30, 2013
in the format 'YYYY-MM-DD' in hive :
2013-12-30
I can do explode and concat to match with given format but converting Dec to 12 is problem.
The UDF is the way to go in hive or impala, but if you have an ETL defined before loading data in HDFS, suggest let the ETL carry out the date conversion. It is important to store dates in format YYYY-MM-DD in order to leverage the TO_DATE() in hive or impala
If all your date is in this format then you can write a UDF which reads that column as string,
then converts the date to a given format. Something like this:
public String convertDate(String dt2) throws ParseException{
SimpleDateFormat incommingDateFormat = new SimpleDateFormat("MMM dd, yyyy");
SimpleDateFormat convertedDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date dt = incommingDateFormat.parse(dt2);
return convertedDateFormat.format(dt);
}
Hope this helps...!!!

Concatenate DateTimePicker in VS2010

How can I concatenate the values of two datetimepicker?
DateTimePicker1 - this is for the the DATE mm/dd/yyyy
DateTimePicker2 - this is for the the TIME hh:mm:ss
I want a value of ... mm/dd/yyy hh:mm:ss in one variable, how can I do that?
One easy way would be
DateTime dt = new DateTime(dtp1.Year, dtp1.Month, dtp1.Day, dtp2.Hour, dtp2.Minute, dtp2.Second);
Please check the order of the parameters, I am writing from memory.
Here dtp1 is a DateTime object from the first datetime picker.

Pattern to parsing datetime with daylight savings time using Joda time

How to use joda time to parse a date time value like this "2011-03-01T01:00:00-07:00"
Does anyone has any code sample to parse the above datetime pattern?
This is an ISO formatted date string. Jodatime supports it as follows:
DateTimeFormatter psr = ISODateTimeFormat.dateTimeParser()
DateTime date = psr.parseDateTime("2011-03-01T01:00:00-07:00")

Resources