How to use moment.js properly? - javascript

I have made a sample page which contains this:
var date = '2015-04-03';
var formate = 'LLLL';
var result = moment(date).format(format);
console.log(result);
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>
And as you can see I have included the moment.js file in order to retrieve data from it and displaying a valid date in the browser. (Here is the link to moment.js that I have inlcuded: link)
But I don't know why it does not work at all! Can u guys help how to use moment.js in proper way ?! Thanks...

You wrote "formate" instead of "format".
Appart from this, you code works fine.
var date = '2015-04-03';
var format = 'LLLL';
var result = moment(date).format(format);
document.write(result);
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>

Related

APPS SCRIPT DATE FORMAT ISSUE

I'm trying to send a table from google sheets to gmail using apps script, i have multiple dates and i want to send in this format "dd-mm-yy", but the problem is that i need to use utilites.formatdate which coverts the cell into string, so when i try to send the email it diplays that the parameters dont match the method, i think this is because in some cells date could be not fullfilled, so theres nothing written in the cell, because of this error i cant send the email, if i write dates in the cells it works but there are cases when they are not fullfilled as i said, how can i solved this problem and send the email no matter if all the dates are filled or not?
var fecha = registro.getRange("c16").getValue();
var fechaF = Utilities.formatDate(fecha, "GMT","dd-MM-YY");
This is the way i tried to change the format, and if there is someting written works but
var fecha2 = registro.getRange("e16").getValue();
var fecha2F = Utilities.formatDate(fecha2, "GMT","dd-MM-YY");
In the second there is nothing so it displays the error
Hope you can help me guys
I'm learning so all kind of advices you give to me are welcome
Try this:
var fecha = new Date(registro.getRange("c16").getValue());
var fechaF = Utilities.formatDate(fecha, "GMT","dd-MM-YY");
Try to initiate Date by adding new DATE() to use the converted string as DATE type and after that you can format it as you want
so your code will be
var fecha = new DATE(registro.getRange("c16").getValue());
EDIT
var fechaF = Utilities.formatDate(fecha, "GMT","dd-mm-yyyy").setNumberFormat('dd-mm-yyy');

Problem with toLocaleDateString and Date.Prototype using MailMerge in Google Sheets

I'm Attempting to create a Merge between some sheet Data, a doc template and a final doc template.
Mainly, I have access to these files like this:
var contratotemplate = DocumentApp.openById(contratotemplateId);
var contratonovo = DocumentApp.openById(contratonovoId);
var ws = SpreadsheetApp.openById(planilhaId).getSheetByName("Contratos Ref Marca");
Than, used this code to pick each Data from a Range that I want.
var data = ws.getRange(3,1,1,ws.getLastColumn()).getValues();
The Problem is here when I try to replacetext with cell information from my sheet trough another function callback (the one that actually replace text with info):
data.forEach(function(r){
CriarMailMerge(
r[30].getDate()+"/"+r[30].getMonth()+"/"+r[30].getFullYear(),
paragrafosTemplate,
contratonovo);
});
The result is this:
11/5/2019 //in contratonovoId field supposed to have the correct date.
I've already tried .toLocaleDateString()
The result is:
June, 11 2019.
Which solves my problem, but I can't find a way to bring that info into Brazilian Portuguese.
Can u guys help me?
I Used this and worked:
r[30].getDate()+"/"+("0" + (r[30].getMonth() + 1)).slice(-2)+"/"+r[30].getFullYear(),
RESULT
11/06/2019

Manipulating video timestamps in JavaScript

How can I do math using video timestamps of the format "00:01:00"?
For example, given a timestamp like, var start = "00:01:00";, how can I add time to start using another timestamp (e.g. var end = "00:05:00") and come away with a result using that same format (e.g. var sum = "00:06:00")?
I have looked into using Moment.js to do this, but that library wants to coerce everything to a Date, which doesn't seem like a good fit for what I'm trying to do.
Actually, you can do that with moment, check below code.
var date = moment("00:01:00", 'mm:ss:SS');
var date2 = moment("00:05:00", 'mm:ss:SS');
date.add(date2);
var element = document.getElementById('app');
element.innerHTML = date.format('mm:ss:SS');
<div id="app"> </app>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js" type="text/javascript"></script>

How can I get date and time separately using jQuery?

In my table date is listed as "2015-07-31 06:02:20". How can I get date and time separately using jQuery?
I used some code but it shows some errors.
var timestamp = new Date(data.responsecusthistory.created_at);
var date = timestamp.toDateString();
Error: Invalid Date
var date = data.responsecusthistory.created_at.split(" ")[0];
var time = data.responsecusthistory.created_at.split(" ")[1];
If you want to have a time string (i.e. HH:MM:SS), try e.g. this:
var timestampTime = timestamp.toTimeString().split(' ')[0];
Anyway, there's no obvious reason why you get the "Error: Invalid Date", as long as
data.responsecusthistory.created_at
is a correct value, such as "2015-07-31 06:02:20". Please consider double-checking this variable.
Try this:
dateString= "2015-07-31 06:02:20";
temp = new Date(dateString);
dateStr= $.datepicker.formatDate('mm/dd/yy', temp );
for getting different formats check the link https://github.com/phstc/jquery-dateFormat.
Using different formats we will get different date, time etc in which ever forms we need it.

How could I parse this date value transferred from model to javascript?

var time =<%:Html.Raw(Json.Encode(Model.AvailableDates))%>;
var parsed = time[0];
alert( parsed );
The pop up shows: "/Date(1174021200000)/". It is not a instance of date. I tried .toString("mm/dd/yy"), Date.parse(parsed), new Date(parsed). Unfortunately, none of these works. I dont wanna let my controller return a formatted value. Is there a way I can parse it on client side? thank you. By the way AvailableDates is a list of datetime in c#.
you can parse it like this:
var date = new Date(<% Model.AvailableDates %>);
Try use moment.js
var time =<%:Html.Raw(Json.Encode(Model.AvailableDates))%>;
var parsed = moment(time);
alert( parsed );
See an example here in jsfiddle

Categories

Resources