Tag Archives

7 Articles

C# useful DateTime extensions

Here are some useful C# DateTime extensions, i hope you enjoy it:

// <copyright file="DateTimeExtensions.cs" company="palow UG">
// Copyright (c) palow UG. All rights reserved.
// </copyright>

using System;

namespace Palow.Library.Extensions
{
    /// <summary>
    /// Extensions for DateTime.
    /// </summary>
    public static class DateTimeExtensions
    {
        /// <summary>
        /// Elasped seconds in comparsion to this DateTime.
        /// </summary>
        /// <param name="dt">This DateTime.</param>
        /// <returns>Timespan of elapsed seconds.</returns>
        public static TimeSpan Elapsed(this DateTime dt)
        {
            return DateTime.Now - dt;
        }

        /// <summary>
        /// Gets the first day of a month from DateTime.
        /// </summary>
        /// <param name="dt">DateTime.</param>
        /// <returns>First day of the month.</returns>
        public static DateTime FirstDayOfMonth(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, 1);
        }

        /// <summary>
        /// Gets the start of the day.
        /// </summary>
        /// <param name="dt">DateTime.</param>
        /// <returns>DateTime to start of day.</returns>
        public static DateTime ToStartOfDay(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
        }

        /// <summary>
        /// Gets the start of the hour.
        /// </summary>
        /// <param name="dt">DateTime.</param>
        /// <returns>DateTime to start of hour.</returns>
        public static DateTime ToStartOfHour(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0, 0);
        }

        /// <summary>
        /// Gets the last day of a month from DateTime.
        /// </summary>
        /// <param name="dt">DateTime.</param>
        /// <returns>Last day of the month.</returns>
        public static DateTime LastDayOfMonth(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddDays(-1);
        }
    }
}

You can call it from any class like this little example, don’t forget to add the using in the top of you class.

            DateTime dateTime = DateTime.Now;
            var startOfDay = dateTime.ToStartOfDay();
            var firstDayOfMonth = dateTime.FirstDayOfMonth();
            var startOfHour = dateTime.ToStartOfHour();
            var lastDayOfMonth = dateTime.LastDayOfMonth();

Elapsed

The function Elapsed returns the time elapsed between now and the DateTime object. If you need a simple stopwatch, you can create a DateTime object, run your functions and use the elapsed function to show how long the operation takes. Be care it has not the accuracy as the stopwatch class, but for longer operations (> 500ms) you should be fine.

FirstDayOfMonth

The function first day of month returns an DateTime object from the given DateTime object from the start of the month. For example: 2018-04-03 converts to 2018-04-01.

ToStartOfDay

Its like the first day of month function, it returns the start of the day at 0:00

ToStartOfHour

Same functionality as start of day, it returns a new DateTime object to the start of the hour.

LastDayOfMonth

It returns from the given DateTime the last day in the month.

Example SHA256 hash in C#

Example SHA256 hash in C#

Here is short code snippet to create a SHA256 hash in C#. The only difficult is to convert from string to byte array and back again. So i have to method, one accept the byte array direct and returns a byte array, the second method handles the convert from string to byte array and back again. Don’t forget to add the required using:

using System.Security.Cryptography;

And here is the function:

        public static string GetSha256(string text)
        {
            if (text == null)
            {
                return string.Empty;
            }

            byte[] message = System.Text.Encoding.ASCII.GetBytes(text);
            byte[] hashValue = GetSha256(message);

            string hashString = string.Empty;
            foreach (byte x in hashValue)
            {
                hashString += string.Format("{0:x2}", x);
            }

            return hashString;

        }

        private static byte[] GetSha256(byte[] message)
        {
            SHA256Managed hashString = new SHA256Managed();
            return hashString.ComputeHash(message);
        }
.Net Core PDF Library from HTML

.Net Core PDF Library from HTML

If you need an PDF library, wich converts an HTML document to a PDF you need a library. There are a lot of libraries on the market, but only a few wich are free and open source. In my projects i used the DinkToPdf library, wich is a wrapper for the webkit engine (the chrome engine) for c#/.Net Core. The library needs as dependency the wkhtmltopdf library, wich you need to add manually. 

Read More