0

I am using time-picker to get the time :

<div class="timePicker-container" >
                        <md-time-picker
                        className="end-time"
                        enable-date="false"
                        (whenChange)="timeSelectionChanged($event)"
                        [disabled]="systemlogsform.value.range==='relativerange'">
                      </md-time-picker>
                      </div>

And timeSelectionChanged is as shown below:

  public timeSelectionChanged(momentTime) {
    console.log('momentTime', momentTime._d)
    }

This contains the below data:

Tue Jun 15 2021 00:17:32 GMT+0530 (India Standard Time)

Now , how will I be able to get only the time part , i.e. 00:17:32 ?

1
  • Do you want the time just to display or do you actually want to store it like that? If this is just formatting to display, use the built in date pipe Commented Jun 15, 2021 at 1:35

3 Answers 3

1

You can convert into Date format and get hours , minutes and seconds from there.

getOnlyTime(dateTimeFormat)
{
  let newFormat = new Date(dateTimeFormat);
  let hr = newFormat .getHours(); 
  let mins = newFormat .getMinutes(); 
  let secs = newFormat .getSeconds(); 

  let timeOnly = hr + ":" + mins + ":" + secs;
  return timeOnly;
 }

You can call the method to retrieve the time.

let timeFormat = getOnlyTime(momentTime._d);
console.log(timeFormat);
Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure what package you are using to convert into the required format, but if you are using 'moment.js' package, you can take a look at this website

Link to moment.js ==> https://momentjs.com/

What you want to do is something like this:

var myDate = "2017-08-30T14:24:03";
console.log(moment(myDate).format("HH:mm")); // 24 hour format
console.log(moment(myDate).format("hh:mm")); // 12 hour format
console.log(moment(myDate).format("hh:mm a")); // use 'A' for uppercase AM/PM
console.log(moment(myDate).format("hh:mm:ss A")); // with milliseconds

Comments

0

const dt = "Tue Jun 15 2021 00:17:32 GMT+0530 (India Standard Time)"

const d = new Date(dt).toLocaleTimeString(undefined, {
  timeZone: 'Asia/Kolkata'
})
console.log(d)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.