How to use bootstrap datepicker's toDisplay function? - bootstrap-datepicker

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.

Related

Reactive mat-datepicker formatting using luxon

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();

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.

MomentJs and Timezone Abbreviations

I am having trouble with the momentjs library
the line
moment("Mon Oct 14 01:00:00 GMT 2013") parses correctly
but the line
moment("Mon Oct 14 01:00:00 BST 2013") throws an invalid date
I have tried building a format string but the zz format which is what I think I need is deprecated, is there a way to make it skip the BST/GMT bit completely as I am only interested in the date
Thanks in advance.
Time zone abbreviations aren't unique, so they cannot be parsed. You can ignore it by putting any non-format character as a placeholder:
moment("Mon Oct 14 01:00:00 BST 2013","ddd MMM DD HH:mm:ss ? YYYY")
But you should be aware that by ignoring it, you'll be assuming the local time zone of the computer where the code is running. Set your computer for another time zone and call .format() on this and you'll see what I mean.
Perhaps you don't care about time zones and just want to reformat this to something else. That's fine, but what if you provide a date that's invalid because of a daylight saving time transition in the computer's local time zone? Your browser will either skip backward or forward depending on which browser your running. To avoid that, you should work in UTC instead of in the local time. Even though your input value is from some other time zone entirely, working in UTC will ensure it doesn't get mangled.
moment.utc("Mon Oct 14 01:00:00 BST 2013","ddd MMM DD HH:mm:ss ? YYYY")

How to convert DateBox's date string into Date_SHORT("YYYY-MM-DD") format?

I use DateBox in Google App Script, and got the value of DateBox Sat Jul 20 2013 00:00:00 GMT+0800 (HKT).
How can I convert it to just be 2013-07-20?
Use Utilities.formatDate() function.

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