LinkedIn Date Decipher

by jscastro76

HTML

<html>

  <head>
    <title>LinkedIn Date Formatter</title>
    <script src="your-script.js"></script> <!-- Link to your compiled TypeScript file -->
  </head>

  <body>
    <div class="container">
      <h1>LinkedIn Post Date Decipher</h1>
      <h3>Just copy your Post URL in this text box</h3>
      <input type="text" id="linkedinUrl" placeholder="Enter LinkedIn URL">
      <button onclick="processUrl()">Get Post Date</button>
      <h3 id="result">
        This post was posted at:
        <p id="output"></p>
      </h3>
    </div>
    <script>
      function processUrl() {
        let url = document.getElementById('linkedinUrl').value;
        let formattedDate = getDate(url);
        document.getElementById('output').innerText = formattedDate;
        document.getElementById('result').style.display = "block";
        
      }

    </script>
  </body>

</html>

CSS

body {
  font-family: Arial, sans-serif;
  background-color: #f4f4f4;
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.container {
  width: 450px;
  text-align: center;
  background-color: white;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h1 {
  color: #0a66c2;
  /* LinkedIn Blue */
  margin-bottom: 20px;
}

input[type="text"] {
  width: 100%;
  padding: 10px;
  margin-bottom: 20px;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
}

button {
  background-color: #0a66c2;
  /* LinkedIn Blue */
  color: white;
  border: none;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 4px;
}

button:hover {
  background-color: #084482;
}

#output {
  margin-top: 20px;
}

#result {
  display: none;
}

TypeScript

function formatDate(dateString: string): string {
  let date = new Date(dateString); // Directly parse the standard format date string

  let day = date.getDate().toString().padStart(2, '0');
  let month = (date.getMonth() + 1).toString().padStart(2, '0');
  let year = date.getFullYear();
  let hours = date.getHours().toString().padStart(2, '0');
  let minutes = date.getMinutes().toString().padStart(2, '0');

  // Get timezone offset in hours and minutes
  let offset = -date.getTimezoneOffset();
  let offsetHours = Math.floor(Math.abs(offset) / 60).toString().padStart(2, '0');
  let offsetMinutes = (Math.abs(offset) % 60).toString().padStart(2, '0');
  let offsetSign = offset >= 0 ? '+' : '-';

  let timezoneOffset = `TZ GMT${offsetSign}${offsetHours}:${offsetMinutes}`;
  let dateFormatted = `${day}/${month}/${year} ${hours}:${minutes} (${timezoneOffset})`;

  return dateFormatted;
}


function getPostId(linkedinURL: string): string {
  let regex = new RegExp('([0-9]{19})');
  let match = regex.exec(linkedinURL);
  if (match) {
    return match[1];
  }
  return '';
}

function extractUnixTimestamp(postId: string): number {
  // BigInt is used as postId is treated as a 64-bit decimal
  let asBinary = BigInt(postId).toString(2);
  let first41Chars = asBinary.substring(0, 41);
  let timestamp = parseInt(first41Chars, 2);
  return timestamp;
}

function unixTimestampToHumanDate(timestamp: number): string {
  // get the utc date from the timestamp
  let dateObject = new Date(timestamp);
  return dateObject.toUTCString();
}

function getDate(url: string): string {
  // extract the unix timestamp from the post's url
  let postId = getPostId(url);
  let unixTimestamp = extractUnixTimestamp(postId);
  // convert the unix timestamp to a human-readable date string
  let humanDateFormat = unixTimestampToHumanDate(unixTimestamp);

  let formattedDate = formatDate(humanDateFormat);
  
  return formattedDate;
}