A guide on manipulating time stamps in javascript/Angular.js to reverse their order

Can someone assist me with reversing the existing time stamp format using Angular.js/Javascript? Here is my code snippet:

$scope.timestamp=2016-12-16 07:02:15 am

The current time stamp format above needs to be reversed as shown below:

$scope.originalStamp=16/12/2016 07:02:15 am

I'm looking to convert the format as indicated. Any help would be greatly appreciated.

Answer №1

Utilizing angular in your project? Don't forget to incorporate $filter

   $scope.timestamp =  '2016-12-16 07:02:15 am'
   $scope.originalStamp = $filter('date')
                          (new Date($scope.timestamp.replace("-","/")),'dd-MM-yyyy HH:mm:ss a');

Try the Updated Demo Here

For detailed code, refer to the following snippet

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="datCtrl">

<p> {{originalStamp}} </p>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('datCtrl', function($scope,$filter) {

   $scope.timestamp =  '2016-12-16 07:02:15 am'
  $scope.originalStamp = $filter('date')(new Date($scope.timestamp.replace("-","/")),'dd-MM-yyyy HH:mm:ss a');

});
</script>

<p>The date filter formats a date object to a readable format.</p>

</body>
</html>

Answer №2

How about customizing the presentation of a date based on its original timestamp? You have complete freedom in how you want to format it.

var enteredDate = new Date(originalTimestamp);

var customizeDateFormat = function(enteredDate) {
    return enteredDate.getDate() + "/" + enteredDate.getMonth() + "/" + enteredDate.getFullYear() + " " + enteredDate.getHours() + ":" + enteredDate.getMinutes() + ":" + enteredDate.getSeconds();
}

$scope.formattedDate = customizeDateFormat(enteredDate);

Answer №3

ng-bind="usercart.date|date:'yyyy-MM-dd HH:mm:ss'"
allows you to customize the date format as needed...

Insert this code

ng-bind="usercart.date|date:'dd-MM-yyyy HH:mm:ss'"

result :

Last Updated Cart 05-01-2017 14:58:38

Answer №4

here is the code snippet:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.date='2016-12-16 07:02:15 am';
  $scope.req = function(date){
          var dateOut = new Date(date);
          return dateOut;
    };
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdaca3aab8a1acbfe3a7be8dfce3f9e3b5">[email protected]</a>" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p ng-bind="req(date) |  date:'dd/MM/yyyy hh:mm:ss a'"></p>
  </body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discovering the unique identifier of an item in Node.js is simple with these steps

I have a delete API which requires the item's unique ID to be passed in for deletion. How can I capture the ID of the item being deleted and send it to the API? Here is the API: app.post("/delete_product", function (req, res) { var data = { ...

AngularJS incorrect scope variable value

When using the HTML5 tag with the scope to set the src, I encounter an issue: <video style="max-height:100%; max-width:100%" controls> <source src="/files/{{item.path}}" type="video/mp4"/> </video> Upon loading this template, the fo ...

How can a controller configure an AngularJS provider by passing options?

How can I pass configuration options to a provider from a controller? Below is an example of how to pass options/configurations to a provider from the controller: provider.js file: app.provider('contactProvider', function() { this.name ...

What could be causing my JavaScript script to only work on the initial HTML element?

I found this JavaScript script online and added it to my HTML. $(function() { var rangePercent = $('[type="range"]').val(); $('[type="range"]').on('change input', function() { rangePercent = $('[type="ran ...

Angular allows for the editing of a table by double-clicking and using an array of objects

I am dealing with an array of objects structured like this: $scope.rows = [{ num1: 56, num2: 78, num3: 89 }, { num1: 56, num2: 78, num3: 89 }, { num1: 56, num2: 78, num3: 89 }, { num1: 56, num2: 78, num3: 8 ...

Leverage the power of Azure CosmosDB in a Single-Page-Application with MSAL authentication using the CosmosDBClient

I am facing a challenge in connecting to an Azure CosmosDB using my Azure AD credentials from a Single-Page-Application. My SPA has a functional login system, ensuring that users are logged in. In Azure, I have successfully registered an app with the scop ...

Unable to decode message property

I recently created some angular modules and controllers, but I'm puzzled as to why the output doesn't display the expressions even though I included "ng-app" and "ng-controller" in my code. script.js /// <reference path="angular.min.js" /> ...

Tips for maintaining rounded bar corners in chartJs when filtering data

I created a JSFiddle with rounded chartJs bar corners: https://www.jsfiddle.net/gcb1dyou. The issue arises when the legend is clicked to filter data, causing the rounded corners to disappear as shown in the image below: https://i.sstatic.net/8NOMa.png Whe ...

Sorting through a pair of items

I currently have two distinct objects in an AngularJS environment $scope.data_1 = {"a":5,"b":6,"c":7,'d':1}; $scope.data_2 = {"a":6,"b":3,"c":2,'d':10}; While I understand how to compare values within a single object using a custo ...

Tips on adjusting the pixel dimensions of an image using a file object

Within a form on our website, users have the ability to upload an image file. To ensure quality control, I've set up validation to confirm that the uploaded file is either an image or gif format. In addition to this, I'm looking for a solution th ...

Exploring ways to loop through a JSON array and embed it into an HTML element

After the ajax request, the data returned is structured as follows: Data = [ ["18/02/2019", "A"], ["19/03/2019", "B"], ["21/05/2019", "C"], ] The ajax request was successful and the data is stored in a variable named Data within a function. ...

Retrieving output from a callback function in one service to another webpage

Currently, I am utilizing Google services for calculating map routes within my application. The specific code I am using is as follows: const directionsService = new google.maps.DirectionsService(); directionsService.route(route, (data, status) => { ...

"Retrieve real-time information from the server directly on the client side

Within my express router, I validate the data submitted on a form and then render another page if the data is valid, passing along the form data. My goal is to be able to access this passed data on the client-side. In the chat.ejs view, I have a chatroom.j ...

Developing a custom altjs flux store to handle data retrieval from an external API

Having trouble figuring out how to create a flux store and action to fetch data from my express API using altjs import $ from 'jquery'; const utils = { myProfile: () => { return $.ajax({ url: '/myProfile', type: &a ...

Passing variables from ExpressJS to JavaScript can be a seamless process

I am struggling with this issue; I am utilizing NodeJS to retrieve a JSON and I must transfer the variable to my page for use by JavaScript. app.get('/test', function(req, res) { res.render('testPage', { myVar: 'My Dat ...

What is the best way to extract numbers from a string using JavaScript?

I am working with a string in javascript that looks like this: var xyz= "M429,100L504.5,100L504.5,106L580,106L570,98M580,106L570,114"; My goal is to extract the numbers and store them in an array. I attempted the following code: var x=xyz.match(/\ ...

Using JavaScript to extract data from a JSON-formatted URL

I am currently facing a challenge with parsing JSON data from a specific URL. Despite my efforts, I am unable to retrieve any information related to the "ask" and "bid" values from this JSON feed. The URL in question is . The structure of the JSON data is ...

The functionality of Angular material allows for a side navigation panel to be

I am a beginner with angular material and have a query regarding the possibility of implementing a sidenav modal. Today, I experimented with the sidenav and it functioned perfectly. However, I am looking for another option where I can prevent the sidenav ...

Delivering a Logo to a Remote Website Based on Specific Conditions

I am in the process of creating a code snippet for a specific user group that has earned the privilege to display our seal on their website. The concept initially appeared straightforward, but when I attempted to implement it on a different site for testin ...

What could be causing my bootstrap dropdown button to malfunction?

Even after selecting an option from the dropdown menu and pressing the button, the value does not change. I attempted to console log it using JavaScript but encountered an error in the Chrome inspector: index.js:26 Uncaught TypeError: Cannot read prop ...