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?

Himabindu
  • 634
  • 8
  • 22
  • Trying to just run your sample code, it gives a syntax error. – Joachim Isaksson Jun 10 '19 at 05: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. – Robin Jun 10 '19 at 05:57
  • Possible duplicate of https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc – Satyam Pathak Jun 10 '19 at 05:58
  • 1
    Possible duplicate of [How do you convert a JavaScript date to UTC?](https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc) – Ajarudin Gunga Jun 10 '19 at 05: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. – Himabindu Jun 10 '19 at 06:04
  • can you print what is getting on `date` variable? – Sanjun Dev Jun 10 '19 at 06:08
  • please refer this question https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc – Hardik Leuwa Jun 10 '19 at 06:11
  • @sanjun dev, i am getting empty string on date variable. – Himabindu Jun 10 '19 at 06:18
  • yes, i am getting date as 2020-03-01 – Himabindu Jun 10 '19 at 06:27

1 Answers1

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>
Sreekanth
  • 3,110
  • 10
  • 22
  • 1
    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. – Himabindu Jun 10 '19 at 06:49