I use moment.js and I just want to add the word "day(s)" after a number. Ex:
Moment(3).function('dd') // 3 days
Any ideas?
You can use format() with some character escaping:
moment(3).format('d [day(s)]'); // 3 day(s)
alert(moment(3).format('d [day(s)]')); // 3 day(s)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
Related
I am using moment js to convert the date to UTC like this
var a = moment.utc('20-Oct-2021').tz('Asia/Kolkata');
a.format()
This results 2021-10-20T05:30:00+05:30
Now I am trying to use access this from Newsland that is from this timezone Pacific/Auckland - In system I changed my timezone to this which is +13.
Now the result for
moment().utc(a).format() is
2021-10-21T02:09:12Z
If you notice the date is 21 instead of 20 which is the actual date stored.
Facing problem with all greater than +-12
Changing your timezone doesn't change the timezone of a since its zone is manually set. You need to use local() to get the time in your timezone.
// Always pass the string format if the string is not an ISO 8601 date
var a = moment.utc('20-Oct-2021', 'DD-MMM-YYYY').tz('Asia/Kolkata');
console.log(a.format());
console.log(a.utc().format()); // in UTC
console.log(a.local().format()); // This is in your timezone
a.tz("Pacific/Auckland"); // Change to Auckland
console.log(a.format()); // Auckland time
console.log(a.local().format()); // In your timezone, as same as above
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.33/moment-timezone-with-data-10-year-range.min.js"></script>
I am trying to calculate the number of days between two dates using moment js.
function (value) {
var expiration= moment(value).format('DDMMYYYY');
var today = moment().format('DDMMYYYY');
var dayToExpiration = moment(expiration- today).format('D[days] ,H[hours]');
console.log(today + " : " + expiration
console.log(dayToExpiration);
The result is:
11102018 : 28102020 //--> 11.10.2018 : 28.10.2018
1 days ,6 hours //why only one day??
Because your dayToExpiration variable should be a moment.Duration object, not a string.
The difference between two datetimes is a duration, not a datetime.
Short answer:
As John Madhavan-Reese stated in his answer, you have to use moment Duration to represent the diffecence between two moments in time.
Issue in the code sample:
In your code you are creating a moment object from the difference between expiration and today. This value is interpreded by moment as the number of milliseconds since the Unix Epoch (see moment(Number)), so you are creating a moment object for a random day around the 1st January 1970 (see the output of moment(expiration- today).format() ). The D token in format() stands for Day of Month, so it gives an "incorrect" output.
My suggested solution:
You can calculate difference using momentjs' diff() then you can create a duration using moment.duration(Number).
Finally you can get your desired output using moment-duration-format plug-in (by John Madhavan-Reese :D)
Here a live sample:
function getDiff(value) {
var expiration= moment(value); // Parse input as momement object
var today = moment(); // get now value (includes current time)
// Calculate diff, create a duration and format it
var dayToExpiration = moment.duration(Math.abs(today.diff(expiration))).format('D[days], H[hours]');
console.log(today.format('DDMMYYYY') + " : " + expiration.format('DDMMYYYY'));
console.log(dayToExpiration);
}
getDiff('2018-10-28');
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/2.2.2/moment-duration-format.min.js"></script>
I am getting errors. this one works for me:
moment.duration(expiration.diff(today))._milliseconds / (1000*60*60*24));
How can I use momentjs to format a number in seconds like
moment(172800).format('hh[h] mm[min]')
to some like
'48h 00min'
in essential I want to count timer pass in hh:mm:ss with hours going way beyond 24.
You can use moment-duration-format plug-in.
You can create a duration from your seconds and then use format method from moment-duration-format to print duration according your needs.
Here a example:
// Create moment duration
var dur = moment.duration(172800, 's');
// Format duration according your needs
console.log( dur.format('hh[h] mm[min]') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
Currently, the last reply timestamp of thread_id in the inbox or sent tab is formatted like this: August 21, 2017 at 5:06 am
I am using bp_message_thread_last_post_date() to display the timestamp.
How do I change this so it displays the last reply in the format 2 days, 11 hours ago?
In the buddypress function you are calling, it echos the value from a similarily named function that applies a filter. You should create your own filter named 'bp_message_thread_last_post_date' to change the format of the date as you wish.
In your functions.php file, add something like the following:
add_filter( 'bp_message_thread_last_post_date', 'fluffyKittenDate', 10, 1 );
function fluffyKittenDate($oldformat) {
// reformat date contained in oldformat here and return the new value
}
Be sure and make the priority of add_filter method high enough to ensure your filter runs last.
I really neeed dd-mon-yy format because my oracle database accepts dates in this format.
Can I validate like that in RegularExpressionValidator like that?
And also, do I have to convert the textbox value to oracle data time format, when using nhibernate?
thanks a lot for help;
try
Regx.ValidationExpression=#"^(([1-9])|(0[1-9])|(1[0-2]))\-((0[1-9])
|([1-31]))\-((\d{2})|(\d{4}))$";
or
you can try this custom validator instead
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
DateTime result;
if (!DateTime.TryParse(args.Value, out result))
{
args.IsValid = false;
return;
} else args.IsValid = true;
}
Is it necessary to force the user to enter the data in the format proscribed by the Oracle database? As long as the user enters a valid date, you can parse the input into a DateTime object and then call ToString("dd-MMMM-yy") to produce a representation of the date in the format required by the database. This gives you the freedom to provide more a user-friendly means of collecting input from the user e.g. a date/time picker type control.
For two digits for the year try:
^([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][Bb]|[Mm][Aa][Rr]|[Aa][Pp][Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][Uu][Ll]|[Aa][Uu][Gg]|[Ss][Ee][Pp]|[Oo][Cc][Tt]|[Nn][Oo][Vv]|[Dd][Ee][Cc])-\d\d$
With four digits for the year:
^([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][Bb]|[Mm][Aa][Rr]|[Aa][Pp][Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][Uu][Ll]|[Aa][Uu][Gg]|[Ss][Ee][Pp]|[Oo][Cc][Tt]|[Nn][Oo][Vv]|[Dd][Ee][Cc])-(19|20)\d\d$
Those expressions mean:
0 or 1 or 2 followed by a number, or 3 plus 0 or 1
next a dash
For the month it 'tries' all the combinations of JAN, JAn, Jan, jan, etc... for all the twelve months
next a dash
Finally it expects two digits for 'yyyy' version or 19 or 20 followed by two digits for 'yy' version