TypeScript: Creating a date filter to show ‘days ago’ in ag-grid

Though I’ve wrote this function for Angular – ag-grid, this can be used as an independent method as well.

image

Code:

 LastUpdated(value: string):string {
let inputDate = new Date(value);
let today = new Date();
let yesterday = new Date(today.setDate(today.getDate()-1));
let numdays = Math.round((today.getTime() - inputDate.getTime()) / (1000*3600*24));
let ret:string = formatDate(inputDate,"MMM, dd yyyy",'en-US', '+4');
if ( inputDate.setHours(0,0,0,0) == new Date().setHours(0,0,0,0)) {
ret = "Today";
} else if ( inputDate.setHours(0,0,0,0) == (yesterday.setHours(0,0,0,0))) {
ret = "Yesterday";
} else if (numdays >= 1 && numdays < 7) {
ret = numdays.toString() + " day" + ((numdays > 1)?'s':'') + " ago";
}
return ret;
}

Angular–ag-grid : how to format a date column?

While you will find multiple solutions for formatting a date in ag-grid on internet/stackoverflow and documentation, here is one method I found handy:

{ field: ‘DateUpdated’, valueFormatter: params => formatDate(params.value, ‘MMM, dd yyyy’, ‘en-US’) }

Find the first and second largest number in a series – without using arrays or functions

This is just a revision of my school memories. Language used is Java.





import java.util.Scanner;

public class testmain {
public static void main(String[] args) {

int large = 0; // there is a possible logic error, ignore
int slarge = 0; // there is a possible logic error, ignore

Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of elements");
int N = scanner.nextInt();

for (int c = 0;c<N;c++) {
int num = scanner.nextInt();

if (num > large) {
large = num;
}

if (num != large && num > slarge) {
slarge = num;
}

}
System.out.println(
"Largest = " + large +
"\nSecond Largest = " + slarge
);
}
}

A quick Sentiment Analysis app using Azure Cognitive Services

Step 1: Create a Text Analytics Resource

From the Azure Portal, create a resource “Text Analytics”

image

Once created, you should be able to get the Endpoint URL and Key from the “Keys and Endpoint” section

image

Step 2: Create a .NET Console application in Visual Studio

Get the Azure.AI.TextAnalytics nuget package for your solution

image

Here you go your sample code

 
using Azure;
using Azure.AI.TextAnalytics;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://mytestoutlookaddin.cognitiveservices.azure.com/";
            string key = "paste_your_key_here";

            var client = new TextAnalyticsClient(new Uri(url), new AzureKeyCredential(key));

            DocumentSentiment mood = client.AnalyzeSentiment("It's a nice day!");
            Console.WriteLine("Mood: {0}", mood.Sentiment );
            Console.WriteLine("Positive: {0}", mood.ConfidenceScores.Positive);
            Console.WriteLine("Neutral: {0}", mood.ConfidenceScores.Neutral);
            Console.WriteLine("Negative: {0}", mood.ConfidenceScores.Negative);
            Console.ReadKey();
            
        }
    }
}

Below is the output expected:

image

Read a Brainyquote and parse the value using xpath in .NET

using System;
using System.Net;
using System.Xml;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WebClient wc = new WebClient())
            {
                string data = 
                    wc.DownloadString("https://www.brainyquote.com/link/quotebr.rss");
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(data);
                var node = xml.SelectNodes("//rss/channel/item[1]");

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("{0} by {1}",
                    node[0]["description"].InnerText,
                    node[0]["title"].InnerText
                    );

                Console.ResetColor();
            }
        }
    }
}