0

 syncCampaignDetails(){ let dateTime = {}; let date = this._root.getElementById("dateField").value; // let time = this._root.getElementById("timeField").value; // dateTime.date = date; // dateTime.time = time; // var newstr = date.replace(IST, ''); let toUtc = new Date(date).toISOString(); // var isoDate = new Date('yourdatehere').toISOString(); console.log(toUtc) }
 <mwc-textfield type="date" required class="date" id="dateField" min="${this.minDate}" max="${this.date}" value="${this.eventStart}"></mwc-textfield> <mwc-textfield type="time" name="appt" required class="time" id="timeField" min="9:00" max="24:00"></mwc-textfield> <button class="syncBtn layout vertical" on-click=${e => this.syncCampaignDetails(e)}> <div class="labelText" id="sync">${__.gettext("Sync")}</div> </button>

The user has to give the event date and event start time and also end time. My requirement is to convert the event date and start time to UTC format. same for end time also.`

syncCampaignDetails(){ let date = this._root.getElementById("dateField").value; let toUtc = new Date(date).toUTCString(); console.log(toUtc) }

` And also date ,start time, end time are different input fields. How to pass the date time.can any one help me?

9
  • Trying to just run your sample code, it gives a syntax error. Commented Jun 10, 2019 at 5:56
  • If you have no problem using third-party modules, i suggest use momentJS for handling time and date. Its pretty easy, everything you need can be done very easily. Commented Jun 10, 2019 at 5:57
  • Possible duplicate of stackoverflow.com/questions/948532/… Commented Jun 10, 2019 at 5:58
  • 1
    Possible duplicate of How do you convert a JavaScript date to UTC? Commented Jun 10, 2019 at 5:58
  • I have already tried using the answers of similar questions in stack overflow. But I am getting toUtc as invalid. the reason what i found is the date which i am passing should be an object. but how do i do that to pass both date and time. some one is saying about JSON.parse or stringify. i am in a little bit confused mode.some help me. Commented Jun 10, 2019 at 6:04

1 Answer 1

1

Here is what you could do.

I modified the snippets to work here in the snippet, but the logic in the syncCampaignDetails is what you would need.

function syncCampaignDetails() { let dateTime = {}; let date = document.getElementById("dateField").value; let time = document.getElementById("timeField").value; dateTime.date = date; dateTime.time = time; let toUtc = new Date(`${dateTime.date}T${dateTime.time}`).toISOString(); document.querySelector('#convertedString').innerText = `UTC Time: ${toUtc}`; } document.querySelector('#sync').addEventListener('click', syncCampaignDetails)
#convertedString { padding: 10px; }
<input type="date" required class="date" id="dateField" min="${this.minDate}" max="${this.date}" /> <input type="time" name="appt" required class="time" id="timeField" min="9:00" max="24:00" /> <button class="syncBtn layout vertical" on-click=${e=> <div class="labelText" id="sync">Convert</div> </button> <div id='convertedString'> </div>

Sign up to request clarification or add additional context in comments.

1 Comment

thank you. it worked for me. But i haven't used object. i have used dynamic value. whenever input value is changed, I am getting the data and converting to UTC format.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.