Reactive mat-datepicker formatting using luxon - momentjs

I'm trying to replace momentjs with luxonjs which is used in mat-datepicker of a reactive form, initially I'm loading the datepicker field with ISO format
'2022-05-10T10:34:31.311-04:00'
for which I used below luxon formatting to save the values
DateTime.fromISO(
this.form?.get('date')?.value
).toISODate();
DateTime.fromISO(this.form?.get('date')?.value)
.plus({ day: 1 })
.toISODate();
When I select different date, the date format is different from my original
Sun May 01 2022 00:00:00 GMT-0400 (Eastern Daylight Time)
How to change this format to ISO using luxon

I solved this issue with a workaround by converting the reactive form date value to ISO format using angular DatePipe
let date = new DatePipe('en-US').transform(
this.form?.get('date')?.value,
'yyyy-MM-ddThh:mm:ss'
);
date = DateTime.fromISO(date).plus({ day: 1 }).toISODate();

Related

How to convert the UTC date into local date in Moment.js?

I have a date in this format "2020-12-16T02:48:00" that came from the server. How can I convert this into local date and time? I tried some code but couldn't succeed.
Below is the attempt that I had made in angular after receiving date from the server.
response.data.map(date=>{
var centralDate = moment( date).zone("-06:00");
date = moment(centralDate).local().format('YYYY-MM-DD hh:mm:ss');
})
If indeed the value is in UTC (as per the title of your question), and it looks like "2020-12-16T02:48:00", and you want to convert it to local time, then you should do the following:
moment.utc(date).local().format('YYYY-MM-DD HH:mm:ss');
That does the following:
Parses the input in terms of UTC
Converts it to local time
Formats it as a string in the given format
Note also that you had hh in your original format. That is for hours in a 12-hour time format and thus you shouldn't use it without also using either A or a to indicate AM/PM or am/pm. Otherwise HH is for hours in a 24-hour time format.
If your issue is that the timezone doesn't change you can resolve using utcOffset (https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/09-utc-offset/) in this way:
response.data.map(date=>{
date = moment( date).utcOffset(-360);
})
Where 360 is the conversion fo the hours in minutes
var d= new Date();
d = new Date(d+ "Z")
I am not an expert in angular but I guess the trouble in your date is the word “T”. May be using string removal function you can remove the word “T” and then it becomes a proper date time value?

Handlebars manipulating date format

I am feeding the following date value into handlebars (this is ISODate format):
2016-03-04T11:24:25.893Z
However on the webpage the date shows as the following:
Fri Mar 04 2016 12:24:25 GMT+0100 (CET)
How do I keep handlebars from manipulating the date format?
myDateVarWhichIWantToPass.toISOString() then pass it to handlebars.

How to use bootstrap datepicker's toDisplay function?

I'm trying to make the bootstrap datepicker display the date in UTC instead of in the browser timezone (as it does by default). I read in the docs that I can specify toDisplay and toValue functions and make them customize how the display is done. As I was implementing the toDisplay function, I first set the date in the element like this:
divElement.datepicker('update', new Date(1420066800000));
Then I was debugging toDisplay() and the first step was to check the value of the first parameter of the toDisplay() function, which is a date object, and make sure it has the same value I supplied when setting the date using 'update', but I noticed that it's different and so my question is why is that so?
The value I supplied when using 'update' is 1420066800000 which is printed as "Thu Jan 01 2015 00:00:00 GMT+0100 (CET)" and the value of the date parameter of toDisplay() is printed as "Thu Jan 01 2015 01:00:00 GMT+0100 (CET)".
Thanks.

How to convert the following time format (2014-10-05T22:25:00-07:00) into required date format in javascript

I want to convert the given time into required time format in javascript using moment.js
moment('2014-10-05T22:25:00-07:00').format('DD-MM-YYYY HH:ss Z');
I am tried the above code to get result "06-10-2014 3.30" but it returns "06-10-2014 10:00 +05:30"
The time you are passing in is
2014-10-05T22:25:00-07:00
which is 5:25am on the 6th of October, GMT. You're then outputting it using the timezone UTC+05:30, which should give:
06-10-2014 10:55:00 +05:30
Unfortunately, you've missed mm from your date string, so you are only seeing:
06-10-2014 10:00 +05:30
Simply change your date format to DD-MM-YYYY HH:mm:ss Z to correct this.

Javascript ASP.net date format without timezone info - timezone offsets

I have a client side JavaScript that generates a date in JavaScript( new Date(2007,5,1)).
I need this date passed through to a hidden field that the code behind can access.
My issue is that when the hidden field is converted into a DotNet datetime, the time is incorrect. This is because the JavaScript is including timezone info from the client browser.
DotNet is then using this info to recalculate the time based on the difference between the server time and the client time.
What i need from the JavaScript is just the year, month and day.
I don't want to pass through 3 int values to my code behind as this will be a major change to the whole app.
What is the best way for me to accomplish this?
If i can set a UTC time with no timezone info I think that might work.
Any help is appreciated.
demo
If I understood it correctly,
you need .toDateString()
var date = new Date(2007,5,1);
document.write(date);
document.write("<br><br>versus<br><br>");
document.write(date.toDateString());
prints
Fri Jun 01 2007 00:00:00 GMT+0800 (Taipei Standard Time)
versus
Fri Jun 01 2007
You can use DateTimeOffset.ParseExact to parse a string to a DateTimeOffset value using the format you specify:
string dateString = "Fri Jun 01 2007 00:00:00 GMT+08:00";
DateTimeOffset date = DateTimeOffset.ParseExact(dateString, "ddd MMM dd yyyy hh:mm:ss 'GMT'zzz", CultureInfo.InvariantCulture);
You have to put GMT in quotes otherwise M will be interpreted as a format character.
Unfortunatelly, it is not possible to ignore part of the string value. If your string includes the name of the timezone you have to split it first and get the part without the description
string dateString = "Fri Jun 01 2007 00:00:00 GMT+08:00 (Taipei Standard Time)";
var parts=dateString.Split('(');
string datePart = parts[0].TrimEnd();
var date=DateTimeOffset.ParseExact(datePart,"ddd MMM dd yyyy hh:mm:ss 'GMT'zzz",CultureInfo.InvariantCulture);
You can build up a string from the javascript Date object you have created - it has getDate(), getMonth() and getFullYear() methods that you can use to build up the exact string you want in the hidden field.
I would recommend to use a format specification in C# when you get the values in the code behind file. Let me explain what I mean -
The date time format for the Date(...) in JavaScript is as follows
"Tue Jun 1 11:12:15 UTC+0530 2010"
which in C# would translate to the following format string -
"ddd MMM d hh:mm:ss UTCzzz yyyy"
with this format string use the DateTime.ParseExact(string <Hidden Field Value>, format, provider) to get the correct value for the datetime in C#.
Use provider as System.Globalization.CultureInfo.InvariantCulture.

Resources