How can we check with moment JS whether value has time present or not.
like, 2021-01-01 // no time. 2021-02-05 01:03:11 PM //time present.
You can parse custom date formats using Momentjs.
Your parser should be YYYY-DD-MM HH:mm:s A
Moment has a function to check if certain moment object is valid so in combination with those two things we can do something like:
if(moment('2019-02-05 01:03:11 PM', 'YYYY-DD-MM HH:mm:s A').isValid())
If date time string in correct format is provided and moment successfully initializes it's object, isValid function will return true and thus you are sure that both date and time is passed in correct format.
For more info about custom date time format strings you can check this out: https://thecodebarbarian.com/formatting-javascript-dates-with-moment-js.html
Edit: I just saw in the comments of your post that you plan on checking string length. Do not do that. It is not secure and it can (and will) produce false positives very often. The way I posted is much better because it will accept just certain format and it must be valid date.
Related
I am using javascript Date.parse to validate if a date string is a valid date time. I have a specific date time input like this: '8/21/2022 1:45: -04:00' when I do Date.parse('8/21/2022 1:45: -04:00'), the seconds part after : is empty, will get a number which means this date format is correct. I also tried using moment.js v2.29.2 moment('8/21/2022 1:45: -04:00').isValid(), will also get true. Just wondering if anyway to make '8/21/2022 1:45: -04:00' format to invalid? Thank you so much
I have a timestamp with this format 09:53:56,07-04-2021
How do I convert it to ISO format ?
I have tried
moment("09:53:56,07-04-2021").format("hh:mm:ss,mm-dd-yyyy")
I'm getting "Invalid date" message
There are two separate function calls, always executed consequently, in your code. The first of those...
moment("09:53:56,07-04-2021")
... is parsing, when Moment constructor tries to create an object from the string parameter it gets. The next call is formatting:
.format("hh:mm:ss,mm-dd-yyyy")
... when Moment uses the string argument to prepare and return a proper representation of its internal date object.
The key is that the first call cannot use the data of the second one. That's why it actually doesn't matter which format you pass there. By the time it's called Moment already misinterpreted the string - and created a messed up date object out of it.
What you need is supplying the correct format into Moment constructor as its second param, like described in the docs. There are two options:
moment('09:53:56,07-04-2021', 'HH:mm:ss,MM-DD-YYYY')
moment.utc('09:53:56,07-04-2021', 'HH:mm:ss,MM-DD-YYYY')
The last one should be used if the datetime provided is always in GMT.
Note that mm in format string means "minutes"; you need M or MM (uppercase) to refer to the "month number" instead. In this case, it's MM, as it's clear that two digits are used.
I have a timestamp from the backend and I want to display it with momentjs per console.log() without timeshifting and completly indepented from my browsers timezone. I read many posts on stackoverflow but nothing works. All of my outputs have some timezone included.
let timestamp = "2019-11-19T07:05:00+01:00";
console.log(moment(timestamp).toISOString(true));
console.log(moment.utc(timestamp).format());
console.log(moment.utc(timestamp).toISOString(true));
console.log(moment.parseZone(timestamp).format());
console.log(moment.parseZone(timestamp).local().format());
console.log(moment.parseZone(timestamp).utc().format());
console.log(moment(timestamp).utcOffset(timestamp).toISOString(true));
console.log(moment.tz(timestamp, 'Europe/Berlin').toISOString(true));
console.log(moment.tz(timestamp, 'Europe/Berlin').format());
console.log(moment.tz(timestamp, 'Europe/Berlin').unix());
console.log(moment.parseZone(timestamp).format('MM/DD/YYYY HH:mm:ss'));
console.log(moment(timestamp).utcOffset("+0100").format('YYYY-MM-DD hh:mm:ss'));
expected output:
2019-11-19T07:05:00Z
The Timezone of the timestamp is: Europe/Berlin
My Browsers timezone is switched to something different.
I don't understand why this simple problem has no easy solution. :)
To meet the requirement you described (keeping the local time while changing the offset) you can do the following:
var result = moment.parseZone("2019-11-19T07:05:00+01:00").utcOffset(0, true).format();
console.log(result); //=> "2019-11-19T07:05:00Z"
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Step by step explained:
moment.parseZone("2019-11-19T07:05:00+01:00") // Parses the string, retaining the offset provided
.utcOffset(0, true) // sets the offset to zero while keeping the local time
.format() // formats the output, using Z to represent UTC
However - You should recognize that these are not the same moments in time. The output timestamp is one hour earlier than the input timestamp. This is explained in the documentation as follows (emphasis mine):
The utcOffset function has an optional second parameter which
accepts a boolean value indicating whether to keep the existing time
of day.
Passing false (the default) will keep the same instant in Universal Time, but the local time will change.
Passing true will keep the same local time, but at the expense of choosing a different point in Universal Time.
Thus, it is usually the wrong choice when you already have a specific point in time, either in UTC or as an offset from UTC (like your example input value).
Instead, you should expect that converting from local time to UTC will indeed change the date and time portion of the timestamp. You can use .utcOffset(0, false), or .utcOffset(0), or simply .utc() to do the conversion correctly.
I have a timestamp 2016-09-14T10:44:55.027Z and I would like to only display the 10:44:55 part but I'm not completely sure how. I have access to the moment library but not sure how to pass this into moment and format it, also how could I add AM or PM?
moment("2016-09-14T10:44:55.027Z").format('hh:mm:ss')
seems to output 11:44:55?
jsFiddle http://jsfiddle.net/eemfu0ym/
Since your input contains the Z suffix, that means the input value is in UTC. However, you're passing it into the default moment constructor, which is local time, thus a conversion occurs.
To keep it in UTC, the simplest way is to just obtain the moment object in UTC mode to begin with.
var m = moment.utc("2016-09-14T10:44:55.027Z")
Once you have that, you can format it however you like:
m.format('HH:mm:ss') // 24-hour clock time
m.format('hh:mm:ss A') // 12-hour time with meridiem (AM/PM)
See the moment formatting docs for other options. Do note that tokens are case sensitive.
I put a UTC .NET/JSON date from .net on client side. When I run the following command:
moment(value.Planet.when).utc()
The returned date from webservice:
"/Date(1469271646000)/"
I get a date in the _d parameter showing the current accurate UTC date with GMT+0300 on right side.
I want to convert this time to local time on the user machine and what ever I do, I always get the time 3 hours back.
I do this:
moment(value.Planet.when).local().format('YYYY-MM-DD HH:mm:ss')
and I get the same date time as the UTC. I don't understand how can I get momentjs to show the UTC time relative to the local time. I checked that the momentjs object is indeed UTC.
I thought that if I pass the moment.utc() function the UTC date that I've got from the webservice (originally from the database), I can just run the local() function and I'll get the accurate hour relative to my area, but it didn't work.
You can use moment(date).format('YYYY-MM-DDTHH:mm:ss');
Eg:- if you date "/Date(1469271646000)/"
ip-> moment(1469271646000).format('YYYY-MM-DDTHH:mm:ss');
op-> "2016-07-23T16:30:46"
Do not use the _d property. It is for internal use only. See this answer, the user guide, or Maggie's blog post on the subject.
As far as you question of how to convert to local time, you don't actually need to convert at all. You're already parsing the input value in local mode, so you can just use it directly:
var m = moment("/Date(1469271646000)/"); // gives you a moment object in local mode.
var s = m.format(); // lets you format it as a string. Pass parameters if you like.
var d = m.toDate(); // gives you a Date object if you really need one
Try to avoid using Date objects unless they're required by some other controls or libraries you're using. Most operations can be done strictly on moment objects.