How to display the date when user types date?

问题: I'm unable to fetch and display the date from "http://worldtimeapi.org/api/timezone/Etc/GMT+7" but i'm able to display the date and time using the inbuilt function but i wa...

问题:

I'm unable to fetch and display the date from "http://worldtimeapi.org/api/timezone/Etc/GMT+7" but i'm able to display the date and time using the inbuilt function but i want to display it using the api.

function MessageHandler(context, event) {
  if (event.message == "date2") {
    context.simplehttp.makeGet('http://worldtimeapi.org/api/timezone/Etc/GMT+7', null, parser);
  }
}

function parser(context, event) {
  var dateJson = JSON.parse(event.getresp);
  var date = dateJson.date;
  context.sendResponse("Today's date is : " + date);
}

function MessageHandler(context, event) {
  if (event.message == "date") {
    context.sendResponse("Today's date is :" + Date());
  }
}

function EventHandler(context, event) {
  context.simpledb.roomleveldata = {};
  MessageHandler(context, event);
}

I'm using gupshup IDE, whenever I give the input date2 I'm getting these lines in bot log

2407: [2019-03-08T09:00:33.249] [INFO] default-Setting Up Bot Event For Bot=> /devnode/Displayinhthedate
2408:[2019-03-08T09:00:33.250] [INFO] default - Setting Up Bot Context For Bot =>/devnode/Displayinhthedate
2409:[2019-03-08T09:00:33.252] [INFO] default-Successfully connected to: /devnode/Displayinhthedate/temp_db
2410:[2019-03-08T09:00:33.285] [INFO] default-Successfully Fetched Data For Key=>bot:global
2411:[2019-03-08T09:00:33.291] [INFO] default-Successfully Fetched Data For Key =>room:1234


回答1:

The problem is here:

var dateJson = JSON.parse(event.getresp);
var date = dateJson.date;

you are getting the date as dateJson.date but there is no key with date in the JSON object.

{
  "week_number": "10",
  "utc_offset": "-07:00",
  "unixtime": "1552031359",
  "timezone": "Etc/GMT+7",
  "dst_until": null,
  "dst_from": null,
  "dst": false,
  "day_of_year": 67,
  "day_of_week": 5,
  "datetime": "2019-03-08T00:49:19.371885-07:00",
  "abbreviation": "-07"
}

The date you can get by using dateJson.datetime. Hope it will help.

  • 发表于 2019-03-09 15:00
  • 阅读 ( 233 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除