how to convert time to eppoch time based on date? - unix-timestamp

I need a javascript function which should take two arguments they are date and time (i.e 9 ,10 ,18,19 etc) and it should return eppoch time for that exact date and time?
please help me?

Try something like this..
var myDate = new Date("July 1, 1978 02:30:00");
var myEpoch = myDate.getTime()/1000.0;
document.write(myEpoch);

Related

Format a date by reversing the position of the year, month and days

The value received from the Date Picker is in the format "02-06-2020" (formatted with mask = "DD-MM-YYYY" in q-date).
I need to convert it to "2020-06-02" format to send it to the server.
I tried the following, but I get undefined.
Example:
let myDate = "02-06-2020";
console.log(date.formatDate(myDate, "YYYY-MM-DD")) //undefined
I would appreciate suggestions for finding a solution.
Thanks.
You can use moment js.
moment(moment('13-01-2020', 'DD-MM-YYYY')).format('YYYY-MM-DD');
or
date.split("-").reverse().join("-");

How to convert local date to UTC?

Can anyone help me with the following:
I have am recording date in my UI, which is IST +5:30
First, I want to convert that date to UTC with start time 00:00
Second, I want to convert that to long time (which I think it is
Unix)
Saved to DB
Third, I want to convert a long time back to UTC in format
MM/DD/YYYY.
This is what I tried so far:
const dateUnix => moment(myMomentObj)
.utc()
.format(DATE_TIME_FORMATS.TIME_STAMP);
The above gets a long time which I don't know if it correct.
const dateMoment = moment.unix(dateUnix)
const formatedDate = dateUnix.format('L'); //which should be in MM/DD/YYYY format
But the formatDate is giving me something like 02/12/15235 which is wrong.
Any help is appreciated.
Thanks in advance.
This code might help you
//input in IST +5:30
var inputDate = moment().utcOffset("+05:30").format();
console.log(inputDate);
//moment.unix outputs a Unix timestamp
var unixTs = moment.utc(inputDate).unix();
console.log(unixTs);
//there is a unix method that accepts unix timestamps in seconds followed by format to format it
var formattedDate = moment.unix(unixTs).format("MM/DD/YYYY");
console.log(formattedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Can we input different date inputs while running the Automation script each time

I am using protractor 5.2.2. We have a requirement of creating a module with unique date so that i cannot create a module with already used date.So when i am running the script , i have to pass different date each time.How we can choose random date in automation.Thanks in advance.
I recommend using chancejs.
var Chance = require('chance'),
chance = new Chance();
console.log(chance.integer({ min: -2, max: 2 }));
would return either -2, -1, 0, 1, or 2.
Please take a look at the chancejs homepage http://chancejs.com/
Below example gives a data between these two years
var Chance = require('chance');
var chance = new Chance();
let bounds = {
min: chance.date({ year: 1983 }),
max: chance.date({ year: 1989 })
}
let date = chance.date(bounds)
console.log(date);
I got 1987-01-21T19:31:32.851Z
1.Random dates in JAVA
Generate random date of birth
if you are using the Excel as data provider
Use the Excel formula's like
=today();
=now();
if you are using java or other languages
use
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyyHH:mm:ss");
Date date = new Date();
C#
https://stackoverflow.com/questions/6817266/get-current-date-only-in-c-sharp
MomentJS is a good option that allows you to set time easily off the current time.
Examples:
moment.format('MM/DD/YYYY'); //gives you current date in mm/dd/yyyy format
moment.format('MM-DD-YYYY'); //current date in mm-dd-yyyy format
moment.add('5','days').format('MM/DD/YYYY'); gives you date 5 days from now

Groovy date format for UTC with milliseconds

I'm having trouble finding a good way of formatting a UTC-time stamp with this format: yyyyMMdd-HH:mm:ss.<three additional digits>
I wasn't able to find any character that represents milliseconds/hundredths, I'm not even sure this is possible, to parse that format that is.
Ideally I'd like to use the parseToStringDate that's part of the Date library.
My plan b is to convert yyyyMMdd-HH:mm:ss to milliseconds and then add the three last digits to that number.
Use yyyyMMdd-HH:mm:ss.SSS
This will get you milliseconds as well.
Test Code:
def now = new Date()
println now.format("yyyyMMdd-HH:mm:ss.SSS", TimeZone.getTimeZone('UTC'))
I would convert it like that:
def now = new Date()
println now.format("YYYYMMdd-HH:mm:ss")
You can try this:
TimeZone.getTimeZone('UTC')
Date date = new Date()
String newdate = date.format("YYYY-MM-dd HH:mm:ss.Ms")
log.info newdate

Given Year, Month,Day and the Week number, is it possible to get the Date in C#? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calculate date from week number
Given Year, Month,Day and the Week number, is it possible to get the Date?
e.g. Year = 2010
Month =Jan
Day = Sun
WeekNumber = 3
output : 2010-01-10
I am trying it in c#
Thanks
I would make it like this:
int Year = 2010;
int Month = 1; //Jan=1, Feb=2, ...
int Day = 0; //Sun=0, Mon=1, ...
int WeekNumber = 3; // greater than 0
DateTime dateValue = new DateTime(Year, Month, 1);
int firstDay = (int)dateValue.DayOfWeek;
dateValue = dateValue.AddDays(Day - firstDay + (WeekNumber - 1) * 7);
I don't think there's something for such date calculations in plain .NET BCL. But there are libraries that can help you, see i.e. Fluent DateTime. Using this library, you can try something like that:
var firstWeekInYearBeginning = new DateTime(2010, 1, 2).Previous(DayOfWeek.Monday); // note 2 to not miss a week if the year begins on Monday
var expectedDate = 3.Weeks().From(firstWeekInYearBeginning);
Based on the APIs here, don't this its possible to initialize a DateTime Object from the information given. You would need to develop an algorithm to get the exact date of the year. A simple strategy would be to get the first Day of the month and based on that, find first Monday of the month. This is the start of WeekNumber 1 for that month, and you can locate your required Week by simpl loop and locate the exact date. You would then know the calendar date you are interested in.
BTW: I am assuming WeekNumber of a year/month starts from the first Monday of that Year/Month. Someone please correct me if I am wrong.
Maybe you should check out System.Globalization.Calendar class. It might be useful.