2013 m. spalio 11 d., penktadienis

Fiunction find difference betwen two dates

 function calcDiff(date1, date2) {
            //Get 1 day in milliseconds
            var one_day = 1000 * 60 * 60 * 24;

            // Convert both dates to milliseconds
            var date1_ms = date1.getTime();
            var date2_ms = date2.getTime();

            // Calculate the difference in milliseconds
            var difference_ms = date2_ms - date1_ms;
            //take out milliseconds
            difference_ms = difference_ms / 1000;
            var seconds = Math.floor(difference_ms % 60);
            difference_ms = difference_ms / 60;
            var minutes = Math.floor(difference_ms % 60);
            difference_ms = difference_ms / 60;
            var hours = Math.floor(difference_ms % 24);
            var days = Math.floor(difference_ms / 24);

            return minutes + ' minutes, and ' + seconds + ' seconds';
        };

2013 m. spalio 10 d., ketvirtadienis

Function format file size

Public Shared Function FormatFileSize(ByVal FileSizeBytes As Long) As String
        Dim sizeTypes() As String = {"b", "Kb", "Mb", "Gb"}
        Dim Len As Decimal = FileSizeBytes
        Dim sizeType As Integer = 0
        Do While Len > 1024
            Len = Decimal.Round(Len / 1024, 2)
            sizeType += 1
            If sizeType >= sizeTypes.Length - 1 Then Exit Do
        Loop

        Dim Resp As String = Len.ToString & " " & sizeTypes(sizeType)
        Return Resp
    End Function

2013 m. spalio 7 d., pirmadienis

Tipų konvertavimas: ToBase64String

Modern applications increasingly use plain text to store and share data, especially in XML and SOAP formats. However, binary data cannot be represented directly in plain text, so one popular method is to convert binary to Base64 format.

What is Base64?

Base64 converts binary data to plain text using 64 case-sensitive, printable ASCII characters: A-Z, a-z, 0-9, plus sign (+) and forward slash (/), and may be terminated with 0-2 “padding” characters represented by the equal sign (=). For example, the eight-byte binary data in hex “35 71 4d 8e 4c 5f db 42″ converts to Base64 text as “NXFNjkxf20I=”.

.NET Convert Methods

Generally, to convert between Base64 and plain text, you should use the .NET methods Convert.ToBase64String and Convert.FromBase64String.

Custom Conversions


However, there may be instances when you want to modify the Base64 standard conversion behavior. For example, applications may use Base64 in file paths or URLs to represent globally unique IDs and other binary data. However, the forward slash is an invalid character in file paths. In URLs, the ‘+’ and ‘/’ characters translate into special percent-encoded hexadecimal sequences (‘+’ = ‘%2B’ and ‘/’ = ‘%2F’), and databases may choke on the % character because it represents a wildcard in ANSI SQL. Therefore, a modified “Base64 for URL” variant exists, where no ‘=’ padding is used, and the ‘+’ and ‘/’ characters are replaced with the hyphen ‘-’ and underscore ‘_’, respectively.


Function getBase64Text(ByVal sInput As String) As String
        Return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sInput))
    End Function

http://www.csharp411.com/convert-binary-to-base64-string/

2013 m. spalio 3 d., ketvirtadienis

Mod and division in Visual Basic

Dim hrs As Integer = allmins \ 60
Dim minutes As Integer = allmins Mod 60

Related operators include the following: