I have a DateTime string which I can assume is in a parsable format using Date.parse() or a 3rd party library. The format could differ based on a user's location and settings. For example, here are some strings I could see: "2014/08/19 16:10:20" or "08/19/2014 4:10:20 PM".
I would like to manipulate the time, say by adding 30 minutes, but also preserve the original format string when I present the new time to the user. I suppose I'm looking for something like a parse function that gives you a DateTime along with the format string it detected. Is there an easy way to accomplish this? 3rd party libraries are welcome, with a preference towards jQuery since I'm already using that.
Note: I don't know what the format is going to be before I parse the datetime string. It can be one of many different formats. (However I probably could build a list of possible format strings.)
Related
While using Angular 12 I have a date as a string in a very custom format, from which I need to produce a Date object. An example of the date strings I have are:
20280328
20271201
To the human eye, these are obviously in the format yyyyMMdd however none of the standard parse functions appear to understand this (not unexpected.)
Other than simply breaking the string using substring, is there a better way to interpret these strings into a Date object?
I was hoping to be able to provide a custom input format for the .parse() function, but this does not appear to be available.
I'm using FormatJS library along with Handlebars to display a list of events that occured in the past. I'm calling for an endpoint on my server's REST API which returns me the list of events in Json, with datetimes to display for each event. ATM I'm saving datetimes in the DB using GMT time zone.
So when I'm getting my Json, I'm handling datetimes like this :
{{formatRelative commentDate}}
My issue is, since the datetimes are stocked in GMT, they display also like that. For example, since I'm on a GMT+2 timezone, as soon as a new event is created and shows up on the list, I see it "happened 2 hours ago" while it should be "a few seconds ago".
So, is there a way I can handle this ? Am I making a mistake in saving datetimes in GMT in my DB, and if so, how would you handle datetimes coming from different timezones and displaying them to people in other timezones ?
Of course I could customize the formatRelative helper to play with getTimezoneOffset and get the wanted result, but I wanted to know if there is something better to do.
Thanks a lot ahead !
The key to understanding your question is what you wrote in the comments:
Getting the Json, containing datetimes in the format 2016-02-28 10:15:53 - that's UTC time
You should ensure the value in JSON is in full ISO8601 format, including the appropriate offset or Z character to indicate UTC: 2016-02-28T10:15:53Z
Without the offset, most implementations will consider the value to be represented in local time, which explains your results.
Thus, the problem is with your server-side code, not your JavaScript code. There may be a client-side workaround you could apply when the date string is parsed from JSON, but really the best solution would be to qualify it at the server.
I want to validate a date in a NodeJS application.
I tried the MomentsJs library, but it seems that the isValid() function ignored all alpha characters, it's too tolerant for me (When I specify the dateformat as YYYY-MM-DD, I expect that the date is a 10-characters-string.):
moment("One,2 and 011, 12-10", "YYYY-MM-DD").isValid() // returns true instead of false
moment("Seppl"); // parsing seems to work with all values?
Is there any way to check dates more strictly?
How can I check if parsing a date fails? (It returns -62167222800000 when printing wrong dates it).
Is there any other better Javascript Data lib that work with different date formats and support date manipulations like MomentJs?
Moment.js library doesnt provide any method validate the date with given format.
moment("<>", dateFormat) will just check the given date but not with given format.
Following lines from momentjs.com
Note: It is not intended to be used to validate that the input string matches the format string. Because the strictness of format matching can vary depending on the application and business requirements, this sort of validation is not included in Moment.js.
http://momentjs.com/docs/#/parsing/is-valid/
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a standard date/time format that can be passed on a URL?
What is a good way for a RESTful resource to accept a datetime object? Specifically, I'm not sure what is a good way to represent the date and time as a query argument in the URL.
I was thinking of doing something like this:
GET /Calls?start=YYYY-MM-DD_HH:MM:SS
I'm using Javascript/jQuery on the Client and Python on the back end, so ideally it would be a format that could easily be written in Javascript and read in Python.
Thanks!
Use the ISO 8601 standard to encode the time argument as a string. It's readable for humans and supported by tons of libraries across many languages.
I'd recommend against using Unix time. Your sysadmins will thank you when they're asked to crawl or parse your web server logs for API calls. Using ISO 8601 will avoid them having to build a secondary step into that process to convert the Unix time number into something that actual humans have to understand.
Most datetime libraries (definitely both Python and JS) follow the same formatting approach, namely format strings. I'd go with any one that just uses digits, and uses descending order of size, i.e. YYYYMMDDhhmmss.
The one other thing to consider before jumping into a format is whether you might need to parameterize by something more akin to a date range, and if including the seconds, minutes, hours, etc. might over-specify the request and make it hard for the client to locate the data they are looking for.
unix time... http://en.wikipedia.org/wiki/Unix_time
why not just YYYYMMDDHHMMSS ?
As long as both sides can follow this format, I don't see any problem
I have an ajax call, which returns datetime. Javascript displays it using client timezone. I not need in any client timezone, I want to show datetime the same as server return. Is it possible?
I get date via:
var d = eval('new' + date.replace(/\//g, ' '));
First off, there's no reason to use eval here (or almost anywhere), and generally lots of reasons not to.
JavaScript has only "local" time (the timezone of the client) and UTC (universal coordinated time, effectively the same as GMT). So your best bet normally is to have the server send you the time in UTC. But in your case, since you want to display the date in the server's timezone, it doesn't really matter whether JavaScript thinks the date is in local time or server time, and it's fine to send it in the server's timezone.
Note that when parsing date strings, JavaScript only recently (as of ECMAScript5) got a standard format for date strings, which is a simplified version of ISO-8601. Details here. Note that some older browsers will not yet support that format.
It's impossible to offer much more guidance without an example of what you're trying to parse.
Diodeus' suggestion also seems to me to make a lot of sense: If you want the date to be displayed in the server's timezone and format, just display the string you're given without interpreting it (again, subject to what the string looks like; I can't immediately come up with a reasonable format that would work with your posted code).