How to remove milliseconds from timestamp in javascript

Questions : How to remove milliseconds from a timestamp

2022-09-22T08:19:58+00:00 2022-09-22T08:19:58+00:00

850

I was asked this question :

Given a timestamp as a long value, write a anycodings_timestamp utility function in Java to drop the anycodings_timestamp milliseconds. For example, given an input of anycodings_timestamp 1274883865399 (actual time: anycodings_timestamp 20100526T14:24:25.399Z), the function would anycodings_timestamp return 1274883865000 (actual time: anycodings_timestamp 2010-05-26T14:24:25.000Z)

I did this :

import java.text.*;
import java.util.*;

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

        long yourmilliseconds = 1274883865399L;
        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
         Calendar c = Calendar.getInstance();


        Date resultdate = new Date(yourmilliseconds);
        c.set(Calendar.MILLISECOND, 0);
        resultdate.setTime(c.getTimeInMillis());
        System.out.println(sdf.format(resultdate)); 
}
}

But it did not give me the right result

Total Answers 3

31

Answers 1 : of How to remove milliseconds from a timestamp

If I understand you correctly there is anycodings_timestamp no need to use Date / Calendar...

long yourmilliseconds = 1274883865399L;
long droppedMillis = 1000 * (yourmilliseconds/ 1000);    
System.out.println(droppedMillis);

1274883865000

Or... if you wish to have date anycodings_timestamp formatting...

Calendar c = Calendar.getInstance();
c.setTime(new Date(yourmilliseconds));
c.set(Calendar.MILLISECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm.ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(c.getTime()));

2010-05-26T14:24.25.000Z

0

2022-09-22T08:19:58+00:00 2022-09-22T08:19:58+00:00Answer Link

mRahman

5

Answers 2 : of How to remove milliseconds from a timestamp

Had same issue had my initial timestamp anycodings_timestamp stored in sq and did anycodings_timestamp sq.setTime(1000*(long)Math.floor(sq.getTime()/ anycodings_timestamp 1000)); that does the job. In my case sq anycodings_timestamp is a sql.Timestamp

0

2022-09-22T08:19:58+00:00 2022-09-22T08:19:58+00:00Answer Link

raja

1

Answers 3 : of How to remove milliseconds from a timestamp

Your code was buggy: You didn't apply anycodings_timestamp the yourmilliseconds to c(Calendar). The anycodings_timestamp fix, to stay within the boundaries of anycodings_timestamp your code:

import java.text.*;
import java.util.*;

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

     long yourmilliseconds = 1274883865399L;
     Calendar c = Calendar.getInstance();        
     c.setTimeInMillis(yourmilliseconds);
     c.set(Calendar.MILLISECOND, 0);

     Date resultdate = new Date(c.getTimeInMillis());
     SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
     System.out.println(sdf.format(resultdate)); 
   }
}

0

2022-09-22T08:19:58+00:00 2022-09-22T08:19:58+00:00Answer Link

jidam

How can I remove milliseconds from timestamp in oracle insert query using TO_DATE

Questions : How can I remove milliseconds from timestamp in oracle insert query using TO_DATE

2022-09-22T08:19:59+00:00 2022-09-22T08:19:59+00:00

677

I have a value like this '2019-05-04 anycodings_timestamp 12:34:20' which I obtain from another anycodings_timestamp database using python code But when I run anycodings_timestamp the below query in oracle,

INSERT INTO sample_table(SCHEDULED_DATE) 
VALUES (TO_DATE('2019-05-04 12:34:20','YYYY-MM-DD HH24:MI:SS'));

I am getting an output like this,

'04-05-19 12:34:20.000000000 PM'

What I want is:

'2019-05-04 12:34:20'

Total Answers 1

25

Answers 1 : of How can I remove milliseconds from timestamp in oracle insert query using TO_DATE

What you are expecting and what you are anycodings_timestamp getting is the same value.

It is just the representation. anycodings_timestamp SCHEDULED_DATE is the timestamp data anycodings_timestamp type and it shows milliseconds as 000..

You can use TO_CHAR to see the value in anycodings_timestamp the format that you anycodings_timestamp want(to_char(SCHEDULED_DATE, 'YYYY-MM-DD anycodings_timestamp HH24:MI:SS')). and anyway you are adding anycodings_timestamp date into timestamp so your timestamp anycodings_timestamp will always have 000.. in the anycodings_timestamp millisecond.

0

2022-09-22T08:19:59+00:00 2022-09-22T08:19:59+00:00Answer Link

mRahman

How do I remove milliseconds from timeStamp?

//Remove milliseconds DateTime date = DateTime. Now; date = DateTime. ParseExact(date. ToString("yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss", null);

How to remove seconds from time in javascript?

Use the setSeconds() method to remove the seconds and milliseconds from a date, e.g. date. setSeconds(0, 0) .

What is timestamps in Javascript?

The timeStamp event property returns the number of milliseconds from the document was finished loading until the specific event was created.