Momentjs get date and set format - javascript

for a weight tracking project i want to get a date from a user in his specific format eg:"05-12-19" , and i want to format it with momentjs to the standard javascript format
This code below is what i tried and think is the nearest to the result i want:
let newDate = moment().format("05-12-19","DD-MM-YYYY");
console.log(newDate); //05-12-19
the result that i was expecting is 05-12-2019 but got something different take a look here (trying to meet stack-overflows quality standards lol)

To create your date, something like this:
let newDate = moment("05-12-19","DD-MM-YY");
console.log(newDate.toDate());
to output your desired format
let newDateStr = moment("05-12-19","DD-MM-YY").format("DD-MM-YYYY");
console.log(newDateStr);

after looking for a while i found a similair answer here.
it wasn't exactly what i was looking for so i'll post here my complete answer:
let newDate = moment("05-12-19", "DD-MM-YY").format("DD-MM-YYYY");
console.log(newDate);
in the moment function the first argument is my date, the second argument is the format of this date, because momentjs doesn't know this format. in the format function i enter the date i want it to format to.

Related

Angular 9 DatePipe: How to ignore timezone format

I am currently using angular 9 DateTime Pipe in order to format my dateTimes.
I am storing the dateTimes in database with DataTimeOffset.
When I am retrieving this dates from server are in this form: "2021-03-30T16:26:52.047+02:00"
I am using this code to apply the format to my dates:
const pipe = new DatePipe('en-Us);
const formatedDate = pipe.transform(date, 'dd.MMM.yyyy HH:mm:ss');
When I am doing this, the pipe is taking the browser time and format my date.
I know that this is the default option, if I am not sending the 3rd parameter to the transform function.
But I have a business need to show the dates as they are...but formatted in the format mentioned above,
My question is: How can I avoid the timeZone convert but still format the dateTime?
You may use var transformedDate = new Date(datepipe.transform(myDate, 'yyyy-MM-dd' + ' UTC'))
After many days of investigation, the only workaround was to use the library "Luxon".
https://moment.github.io/luxon/docs/manual/zones.html
With this library I can do something like this:
var keepOffset = DateTime.fromISO("2017-05-15T09:10:23-09:00", { setZone: true });
Which will keep the offset intact.

moment js change the date portion of a datetime

Using the moment.js library say i have a datetime with today's date and i would like to replace only the date part of the datetime with another value and keep the time portion the same
I don't want to subtract or add days etc - i have a 3rd party time picker that when you select a time it creates a datetime that is always the current day. I need to send back to server a different datetime - the date is different but keep the time portion from the picker.
example code:
let myDate = "2019-03-15T00:00:00"
let selectedDateTime = "2019-04-04T12:30:00"
expected result would be:
"2019-03-15T12:30:00"
Thank you
The following should solve your problem:
let myDate = moment("2019-03-15T00:00:00")
let selectedDateTime = moment("2019-04-04T12:30:00")
selectedDateTime.date(myDate.date());
selectedDateTime.month(myDate.month());
selectedDateTime.year(myDate.year());
As #JeremyThille suggested, you should take a look at the documentation.

How to get previous month and expected format from month picker using moment JS

I am using jquery.ui.monthpicker library. For month picker I am getting date like 07/2017. From this date string I need to calculate previous month and formatted like 1707 using moment js library.
any help would be appreciated.
This code may solve your problem.
moment("07/2017", "MM/YYYY").subtract(1, 'months').format('YYMM');
DEMO at https://jsfiddle.net/nffswx75/
var dt = "07/2017";
alert(moment(dt,"MM/YYYYY").format('YYMM'));
alert(moment(dt,"MM/YYYYY").add(-1, 'months').format('YYMM'));
alert(moment(dt,"MM/YYYYY").subtract(1, 'months').format('YYMM'));
You can let moment create a date object from a string by telling it what format your date is in.
let dateString: string = "07/2017";
var date = moment(dateString, "MM/YYYY").subtract(1, 'month').format("YYMM");

MomentJS change the format from YYYYMMDD

I have this format of date YY which looks like 20141229 I'm trying to get it into Timestamp format
I tried moment(date).format('x') but I get "Invalid date", not sure how to make this done, any help please ?
thanks!
moment constructor takes format as second parameter, so moment('20141229', 'YYYYMMDD') will give you valid moment object. and calling .unix() should give you timestamp.
Docs
Try separating segments with a space, for example
var date = "2014 12 29"
var stamp = moment(date).format("x")

How to get difference of saved time and current time in jquery?

I want to get the time difference between saved time and current time in javascript or jquery. My saved time looks like Sun Oct 24 15:55:56 GMT+05:30 2010.
The date format code in java looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
How to compare it with the current time and get the difference?
Is there any inbuilt function in jquery or javascript??
Any suggestions or links would be appreciative!!!
Thanks in Advance!
Update
Date is stored as varchar in the DB. I am retriving it to a String variable and then change it to java.util.Date object. The java code looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
This date object was sent to client. There i want to compare the saved date with current date and want to show the time difference like 2 secs ago, 2 hours ago, 2 days ago etc... like exactly in facebook. I have gone through some date to timestamp conversion tutorial in java script and now i can get the difference in timestamp. Now, i want to know how i shall change it to some format like "2 secs or 2 days or 24 hours"??. Or, how i shall change it back to date format???
Convert them into timestamps which are actually integers and can get subtracted from each other. The you just have to convert back the resulting timestamp to a javascript date object.
var diff = new Date();
diff.setTime( time2.getTime()-time1.getTime() );
You dont need to explicit convert, just do this:
var timediff = new Date() - savedTime;
This will return the difference in milliseconds.
jQuery doesn't add anything for working with dates. I'd recommend using Datejs in the event that the standard JavaScript Date API isn't sufficient.
Perhaps you could clarify exactly what input and output you're aiming for. What do you mean by "the difference?" There is more than one way to express the difference between to instants in time (primarily units and output string formatting).
Edit: since you said you're working with jQuery, how about using CuteTime? (Demo page)

Categories

Resources