0

Im trying to work on getting the Date value of Timestamp. I want to get the UTC equivalent value of the date since current working implementation uses my local Timezone. I found this very cool method that does it toUTCString()

Given the following line of code:

console.log("new Date(data[i].time).toUTCString()" , new Date(data[i].time).toUTCString());
console.log("new Date(data[i].time)" , new Date(data[i].time))
tempData.push(Object.assign({}, data[i], {'date': new Date(data[i].time).toUTCString()}));

Where console log returns the following and I get the converted result:

new Date(data[i].time).toUTCString() Wed, 06 Jun 2018 03:50:00 GMT
new Date(data[i].time) Wed Jun 06 2018 11:50:00 GMT+0800 (+08)

Gives me this error message:

Uncaught (in promise) TypeError: d.getTime is not a function

As further research, some SO answers point out the problem is because its a string value, but I think im using the right method to convert it to UTC.

Am I missing something here? Thanks!

I have a file autogenerated when I ran webpack, and this is the lines where I am directed base on the error.

var discontinuousIndexCalculator = (0, _utils.slidingWindow)().windowSize(2).undefinedValue(function (d, idx, di) {
    var i = di;
    var row = {
        date: d.getTime(),
        startOf30Seconds: false,
        startOfMinute: false,
        startOf5Minutes: false,
        startOf15Minutes: false,
        startOf30Minutes: false,
        startOfHour: false,
        startOfEighthOfADay: false,
        startOfQuarterDay: false,
        startOfHalfDay: false,
        startOfDay: true,
        startOfWeek: false,
        startOfMonth: false,
        startOfQuarter: false,
        startOfYear: false
    };
    var level = evaluateLevel(row, d, i);
    return _extends({}, row, { index: i }, level);
});
  • 2
    where are you using `getTime()`? – Satpal Jun 11 '18 at 08:09
  • I think that occurs just that d is not a Date object – Terry Wei Jun 11 '18 at 08:11
  • I dont use `getTime()` in my code, but Ill paste the strip of code above where I found it. –  Jun 11 '18 at 08:15
  • @JohnReyTanquinco Nop. Are you using any external library ? It looks like you're using an external library who expect a date object in data/parameters/config and you're not passing only dates..? – Alexis Jun 11 '18 at 08:19
  • Im using this implementation https://github.com/rrag/react-stockcharts-examples2/blob/master/examples/CandleStickChartWithInteractiveIndicator/src/utils.js but I have my custom actual code found here https://pastebin.com/7TY0utp3. And constant `parseDate` is not being used. –  Jun 11 '18 at 08:23
  • @Alexis Thanks for your suggestion, maybe you can post a basic code in the answer section, so I can understand it more. Im just starting to love javascript and im learning a lot. Appreciate your response! –  Jun 11 '18 at 08:45
  • @JohnReyTanquinco I've not enough details for write a correct answer. The tempData array is used by your library right ? – Alexis Jun 11 '18 at 08:47
  • @Alexis Im not sure of your question but basically, I am calling `getData().then(data => {})` to get the data from the function defined. –  Jun 11 '18 at 08:51

2 Answers2

0

You can try something similar to this.

const data = [
 {
  time:new Date().getTime()
 },
 {
  time:new Date().getTime()
 },
 {
  time:new Date().getTime()
 },
 {
  time:new Date().getTime()
 },
 {
  time:new Date().getTime()
 }
];

const dataWithString = [];

for(let i = 0; i< data.length;i++){
   console.log(new Date(data[i].time).toUTCString()); //Something similar has you have
   
   //Solution 1 create an other array and save the index to map each array;
   dataWithString.push({
    dateString:new Date(data[i].time).toUTCString(),
    index:i
  });
  
  //Solution 2
  data[i].dateString = new Date(data[i].time).toUTCString();
}

console.log("Results");
console.log(dataWithString);
console.log(data);
Alexis
  • 5,681
  • 1
  • 27
  • 44
  • Im getting `Cannot read property 'getTime' of undefined` in Solution 1, while getting the same previous error on solution 2. As observation, what I think here, is that the implementation of my code expects a date object like this `Wed Jun 06 2018 11:50:00 GMT+0800 (+08) {}` and not a string `"Wed, 06 Jun 2018 03:50:00 GMT"` that is being generated by `toUTCString()` method. Hm, im not sure but is there a way I can convert back the UTC string value into date object? –  Jun 11 '18 at 09:37
  • Yeah with `new Date("UTC String");` – Alexis Jun 11 '18 at 11:41
0

Found exactly what I need in both the answer and Tim's comment here. Might not be the best solution but this works. This actually is giving me a Date object that I need, not a string value.

I use: new Date(new Date().toUTCString().substr(0, 25));