msg object is not readable info - javascript

Good day people of Stackoverflow...
I have a serious problem with the info I`m reading of my weather API.
I get info into a function node that exports the sunset and sunrise times,
now the problem is that it gives me the info in a weird way.
EG. Object:
sunrise: 1572060152
sunset: 1572106754
now if I click on the number it changes to a HEX. And if I click on it again it changes to something else, not sure what u call it but it looks like this "2019-10-26T03:22:32.000Z"
And then if I click it again I get the info that I require 10/26/2019, 5:32:32 AM [UTC+2]
I need to get the time extracted from that info but it doesn't matter what I try the outcome is always the 10 digit number...
Please help.

The OpenWeather node is returning timestamps in what is known ans epoc format. Epoc time is the number of seconds since 1st January 1970. This is basically how all computers work with time.
The Node-RED sidebar is just being helpful, in that when it sees a number that is in the right range, it will automatically convert it to different things when you click on it, in this case it is converting first to a hex representation (0x5DB47202) and then to a human readable timestamp (using what ever timezone your browser is using).
What you do with that value entirely depends on what you are going to do with it next, but assuming you want to display it to a user somewhere (e.g. in the Dashboard UI) then have a look at the node-red-contrib-moment node as this has lots of options for working with timestamps and converting them into other formats.

Related

MomentJS .date() issue

Could anyone please explain me why moment("2013-04-22 00:00:00+07:00").date() returns 21?
For example moment("2013-04-22 00:00:00+02:00").date() returns 22.
I would be interested in getting the date without taking into account any timezone info in the string from which the moment object is built, i.e. always 22 in this particular example.
I think I figured it out: moment("iso8601_string") parses/converts that string into a local time. In my case, I'm in the UTC+2 timezone, so parsing strings containing "+02:00" (or "+01:00") and then calling .date() returns 22 because the time stored in the moment object matches my local time.
However, when parsing a string with e.g. "+07:00", the time stored in the moment object will be my local time, in which case the date will actually be different, a day before.
I think also my original requirement was convoluted and based on an incorrect assumption. My use-case was that I got the datetime from some UI widget always as date+time, but I needed only the date. Writing a unit test which parsed a string containing "+07:00" caused the date to be "incorrect". However this was an incorrect assumption, because (at least in my use-case) the datetime from the UI widget will always be in the local time so the code won't ever parse a string containing "+07:00".
I'm not sure that a use-case as I originally stated does exist in the real world:
you get a string containing a date in another timezone
you need to take the date out of it, but in the timezone of that date (why?)
I guess normally whenever getting a date from somewhere remote the convention is to always get UTC, that way the point of reference is clear and you can convert it further locally. So probably the case when you get a date in a random timezone and need the date from it, in that timezone, isn't common. Still, as an academic question, I don't know how I would be able to get the date out of it in that case :)

SQL Server Date (not Datetime) to JavaScript Date - one day off

I've tried tons of things to fix this, but the problem continues to vex.
I have a Date field in SQL Server (not Datetime, because time doesn't matter), and a Java API (Spring actually) serves this field to my Ember front-end, where a user can edit the field (using an ember-pikaday input, but that may be immaterial).
The problem is that when I switch to edit mode, the date shows one date before, and if I save, that one date before is what gets saved. If I look at what the API is returning, it looks like the following:
"2015-02-03T05:00:00.000+0000"
I'm still very much learning JavaScript (and Ember), so it looks like there's some local time zone information being used automatically (I'm in EST, which is GMT-5) and that's why it's returning the 5AM time.
I really don't want it to do anything at all with time, but as JavaScript date objects by definition include time info, I'm not sure how to get rid of it. I've tried setting the time to noon so that even with a five-hour difference, it will be the same day, but since the database doesn't save the time, the next time I refresh the page, I'm back to square one.
I've run out of things to Google, which is why I posted here. I'm sure I'm missing something obvious and easy.
EDIT: I tried creating a new variable to get just the date part and using that throughout, but when I go into Edit mode, where my Pikaday input comes into play, it still shows the date before, so I'm suspecting that's the cause of the issue, but I haven't seen hardly anyone else complaining of this.
Here is a similar ember.js question:
Ember.js dates a day early
Also make sure your javascript ISO-8601 date has the timezone. I had a similar issue with web api where the ISO-8601 dates were not sending any timezone info and no Z for UTC and the receiver assumed they were not converted to UTC and made them a day earlier '2012-07-27T18:51:45.53403Z // UTC'. You could use Fiddler or Chrome dev tools to see the format of date you are sending to the server make sure the Z or offset '2012-07-27T11:51:45.53403-07:00 // Local' is there.

Save time of day as number in mongodb, but display in a human format (using meteor autoform)

In a form, I'm asking for a start time & end time, which I'll be using in a script later on.
I figured in MongoDB, a time of day is best stored as the # of seconds since midnight (per How can I store time-of-day in MongoDB? As a string? Give arbitrary year/month/day?).
My question is: how can I display a human-readable time in autoform (e.g. 7:30pm) yet still save it as a number in mongodb & have proper client-side validation (make sure time is before 8:00pm)? I figure I could either use a datetime object & subtract the seconds since 1970, or I could parse the time string & do math on the hours, minutes, AM/PM.
Thoughts on methods? & where to put the math hooks in autoform? This seems like something folks a lot smarter than me have probably already figured out!
new Date(<unixTimeStamp>) will get you a javascript Date object which can be easily played with using libraries like moment (https://github.com/moment/moment)
Edit: also, to get the proper timestamp you can do +Date.now(). This jives with mongodb's date type

getting time zone when entering state and country (time zone to use in ics file)

Getting time zone when giving state and country in text box
is there any way to get the time zone of that place with this two values?
the input will be like
var state = 'New York' ;
var country = 'United States';
result should be
America/New_York
OR
get local time zone of the current browser?
I need this time zone to use in ics file.
This is impossible, for the simple reason that quite a few states span more than one timezone. See Wikipedia's list. Outside the US it can get even more complicated; I believe there are some cities that span multiple timezones.
You could cobble together guesses per state (e.g. by using that list), but if this is for figuring out the user's timezone, you'll probably have better luck just comparing the client clock with the server's UTC time and estimating based on country.
edit: Note that there's no way to ask the browser for the current timezone, either, and you can't guess reliably based on the current time, because there are many timezones where it's the same time right now but where DST is different. Your best bet is to find all the possible current timezones, estimate based on the user's location (which you also have to guess!), and just ask as a last resort.
No, there is no pre-defined methods exists.
For this you need some external web services. If you're interested you can create your own API using the information provided here in wikipedia
Updates: Based on your comments "get local time zone of the current browser
var date = new Date();
returns 12:38:05 GMT+0530 (India Standard Time)
To pick the time within the bracket use
date.toTimeString().match(/\(([^)]+)\)/)[1];
returns India Standard Time
Check this JSFiddle
But this is not you expected, however you should try Auto detect a time zone with JavaScript and for updated version try this jsTimezoneDetect
GeoNames provides a data dump that you can use:
http://download.geonames.org/export/dump/
This is per skylarsutton's response in the following previous post with a similar question (but not specific to js or query)...
I need a mapping list of cities to timezones- best way to get it?

Asp-net web api output datetime with the letter T

the data in the DB look like this
2011-09-07 14:43:22.520
But my Web API outputs the data and replace the space with the letter T
2011-09-07T14:43:22.520
I can replace the letter T with a space again in jquery, but can I fix this problem from the Web API (make the web api output the original data?)
I also do not want the miliseconds at the end. How can I get rid of them?
The format of how you see the date in the database is usually irrelevant, because it should be passed into .Net as a DateTime - not as a string. (If you are storing it as a varchar in the database, you have a bigger problem.)
ASP.Net WebAPI is returning the value in format defined by ISO8601 and RFC3339. This is a good thing, as it is a recognized machine-readable format. You probably don't want to change it.
If you really want to change it, you would need to implement a custom JSON.Net JsonConverter, deriving from DateTimeConverterBase. This is discussed here and here.
But instead, you should consider how you are using the actual result in your client application. You mentioned jQuery, so I will assume your consumer is JavaScript. In many browsers, the ISO8601 value that you have is already recognized by the JavaScript Date constructor, so you might be able to just do this:
var dt = new Date("2011-09-07T14:43:22.520");
But this won't work in all browsers. And Date doesn't have a whole lot of flexibility when it comes to formatting. So instead, you might want to consider a library such as moment.js. With that in place, you can do this:
var m = moment("2011-09-07T14:43:22.520");
var s = m.format("YYYY-MM-DD HH:mm:ss"); // output: "2011-09-07 14:43:22"
Please note that the format string here conforms to moment.js, not to .NET. There are differences in case sensitivity. Please refer to the moment.js documentation for details.
One other thing - since the value you provided doesn't have either a Z at the end, nor does it have an offset such as -07:00, then I assume it came from a DateTime whos .Kind value is DateTimeKind.Unspecified. You should be aware that when this gets sent into JavaScript (or anywhere else for that matter), there is no information about what time zone is represented. JavaScript will assume the local time zone of the browser.
If that's not what you had intended, then you need to store UTC values in your database, and make sure they have DateTimeKind.Utc so they get serialized with a Z at the end. JavaScript will normalize this to the browser's time zone, but you will still be talking about the same moment in time.
Alternatively, you could use a DateTimeOffset type - which would serialize with the specific offset. JavaScript will still normalize this to the user's time zone.

Categories

Resources