Javascript - Remove Milliseconds from Date Object

JavaScript - remove milliseconds from date object

Try to split your toISOString() with . using String.prototype.split(), grab the 0th index and concatenate the literal Z with the output. This way you can simply ignore the 1st index that contains the milliseconds value. Hope this helps :)

var sun = new Date();sun.setDate(sun.getDate() + (7 - sun.getDay()));sun.setHours(9);sun.setMinutes(0);sun.setSeconds(0);console.log(sun.toISOString().split('.')[0]+"Z");

.getTime() Alternative without milliseconds

Simple arithmetic. If you want the value in seconds, divide the milliseconds result by 1000:

var seconds = new Date().getTime() / 1000;

You might want to call Math.floor() on that to remove any decimals though:

var seconds = Math.floor(new Date().getTime() / 1000);

Is there a different function I can use that will give me only the minutes since 1/1/1970, I just dont want the number to be as precise.

Certainly, divide the seconds by 60, or divide the milliseconds by 60000:

var minutes = Math.floor(new Date().getTime() / 60000);

var milliseconds = 1426515375925,
seconds = Math.floor(milliseconds / 1000), // 1426515375
minutes = Math.floor(milliseconds / 60000); // 23775256

How to remove milliseconds from Date Object format in Java

Basic answer is, you can't. The value returned by Date#toString is a representation of the Date object and it carries no concept of format other then what it uses internally for the toString method.

Generally this shouldn't be used for display purpose (except for rare occasions)

Instead you should be using some kind of DateFormat

For example...

Date date = new Date();
System.out.println(date);
System.out.println(DateFormat.getDateTimeInstance().format(date));
System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date));
System.out.println(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(date));
System.out.println(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(date));

Will output something like...

Thu Jan 30 16:29:31 EST 2014
30/01/2014 4:29:31 PM
30/01/14 4:29 PM
30/01/2014 4:29:31 PM
30 January 2014 4:29:31 PM

If you get really stuck, you can customise it further by using a SimpleDateFormat, but I would avoid this if you can, as not everybody uses the same date/time formatting ;)

I would like to remove seconds and milliseconds from my date

Convert String to the Date Object. And then extract whatever format you want

  $scope.time = '17:34:14.965';
var timeTokens = $scope.time.split(':');
var newDate= new Date(1970, 0, 1, timeTokens[0], timeTokens[1], timeTokens[2]);
$scope.hourAndMin = $filter('date')(newDate, 'HH:mm');

var app = angular.module("app", []);app.controller("ctrl", function($scope, $filter) {  $scope.time = '17:34:14.965';  var timeTokens = $scope.time.split(':');  var newDate= new Date(1970, 0, 1, timeTokens[0], timeTokens[1], timeTokens[2]);  $scope.hourAndMin = $filter('date')(newDate, 'HH:mm');
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><div ng-app="app" ng-controller="ctrl">  {{hourAndMin}}</div>

TypeScript: Remove Seconds from Date Object and Reconvert back to Date?