![]() This code is working, but i think it's can be write more simple using moment.js. It is hard to imagine writing JavaScript without it.I try to convert a dateTime string format( dd/MM/yyyy HH:mm:ss), to local user time. The moment.format() function is so flexible, In particular, date formatting is usually the first reason Time zones, localization, addition and subtraction, durations: Moment makes mostĬomplex date tasks easy. datetimefrommoment moment.utc ().utcOffset ('+0530') and then convert it to string so I could save it to firebase realtime database like this: datetimefrommomentstring datetimefrommoment.toString () after sometime I get that datetime (which is in string format) from database to do some calculations. Moment is an extremely powerful library with an unbelievable breadth of features. Prints "vor einer Minute" console.log(duration.humanize( true)) Moving On In your customer's language: const moment = require( 'moment') The humanize() function is locale aware, so you can render durations True, indicates whether the duration is positive or negative ('in a minute' vs 'a minute ago'). The humanize() function takes an optional parameter suffix that, if set to Moment.duration(m2.diff(m1)).humanize( true) // 'a minute ago' For example: const moment = require( 'moment') Ĭonst m1 = moment( new Date( '1 2:04:03')) Ĭonst m2 = m1.clone().add( 59, 'seconds') Ĭonst duration = moment.duration(m1.diff(m2)) ĭuration.humanize( true) // 'in a minute' That won't work because myDate is set to be a Date type. The output format should be specified when calling. The moment.diff() function returns a Moment duration object that represents the difference between two moments. let myDate:Date moment (dateString,'YYYY-MM-DD').format ('DD-MM-YYYY') Basically, when you call moment (), you should specify the format your current date is in, not the output format that you want. h: hour of the day on 12 hour clock, '2'ĭifference between two times into something human-friendly like 'a minute ago'.hh: hour of day on 12-hour clock, zero-padded, '02'.HH: hour of day from 0-24, zero-padded, '14'.Here's a list of commonly used time format tokens. For example, here's how you can display a date's time in 2:04pm format: const moment = require( 'moment') Ĭonst m = moment( new Date( '1 14:04:03')) You can display just the date component, just the time component, or a combination The format() function is very flexible, so ![]() M.format( 'LL') // 'June 1, 2019' // Set the Moment locale to Germany moment.locale() // 'en' let m = moment( new Date( '1')) The L and LL tokens to get a locale-specific formatting of the date. You can escape moment tokens using square brackets. However, you'll get a surprising output: 'T126 1st of June'. Naively you might try the below format: // 'T126 1st of June' For example, if you want a more elaborate date like 'The 1st of June', Sometimes you want to add text to the format string that conflicts with a moment Here's some common date formats and how to express them in Moment format strings:
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |