danza

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);
        }
You can hire me!

You can hire me!

by danza 0 Comments

If you search for a professional software developer, you have found one! On this page you can look what i can, wich are my skills and projects i have done.

Don’t forget the to checkout the company page. I will see you at a new interesting project!

PHP PDO jQuery AJAX Example

PHP PDO jQuery AJAX Example

I have found a very nice tutorial, how to use PHP, PDO, jQuery and the ajax technology to create, edit and delete records on a database table. I have copied the script and had make an online demo page from this project.

Its like my own German article how to use PHP and Ajax to edit a table on the MySQL database. Its interesting, how different the concepts are to create a editable MySQL table.

Feel free to compare the different scripts on this demo sites:
Preview the PHP PDO example from programmerblog.net.
Preview the mysql ajax edit table example from my blog.

Don’t forget to checkout the website from programmerblog.net, there are many good articles.