Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format date/time using .NET instead of JavaScript function #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

y2k4life
Copy link

If you are interested.

The thinking might be, "I'm working in .NET with Blazor why then date and time formatting done in some other fashion that is not congruent with .NET." At least it was for me. One less thing to learn when I know .NET formating.

This is to address that issue, make formatting similar to .NET DateTime. My solution is to create .NET methods and call them from JavaScript. The challenge was having to create a date/time string that could be passed between JavaScript and .NET that would not assume the value as UTC and eliminate any type of conversion. The time zone offset also needs to be passed back to .NET methods for the same reason and to maintain the browser time without converting to server time. I used DateTimeOffset to solve some of these challenges along with formatting the time in JavaScript to ISO 8601 WITH the time zone of. One might exist but I could not find it, anything I found would convert to UTC.`

Now that the converting a date/time to a string is using the ToString() of the DateTime all of the standard .NET formatting should work. Standard date and time format strings.

I also incorporated a means to use the methods on the DataTime by way of reflections. For example, Format="ToShortDateString" to call the ToShortDateString of the DataTime object. You can call any of the methods, ToLongDateString, ToLongTimeString, or any other that returns a string. This is not completely tested and might need some work.

I also did some updates to .NET 6.0 but not a complete project renaming.

If you are interested, I can continue to clean up the code for .NET 6.0 and test other DataTime string methods.

Guy Boicey added 3 commits February 28, 2023 13:53
Create .NET methods to format date/time. Calling those functions from JavaScript. This replaces node-dateformat functions. Makes formatting exactly the same as .NET instead of a JavaScript library.

1. Change HTML to be semantic with HTML5 using data- and span instead of custom tags and attributes
2. Used OnAfterRenderAsync with a flag to indicate the component was created instead of using HTML observations
@dustout
Copy link
Owner

dustout commented Mar 1, 2023

Hello Guy,

In hindsight I should have named this library BlazorServerTime.

The issue with calling DotNet.invokeMethodAsync for each html time node is that it can be a big hit when there are a lot of times on one page (for example in a table).

@y2k4life
Copy link
Author

y2k4life commented Mar 1, 2023

And I'm just now seeing that as I take this out of the lab and try it for real use. A column with date/times is all blank then the date time pops in after rendering. Something to work on.

@y2k4life
Copy link
Author

y2k4life commented Mar 1, 2023

Dohhh! Two columns one is using the updates the other is just showing the date. Typical programming, I solve one problem and I cause another.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants